https://www.urbanpro.com/rr/992771
Python Tutorial by Siva Krishna
Friday, 22 June 2018
Wednesday, 30 May 2018
To print the multiplication Table
Example:
num=int(input("enter number: "))
print("the multiplication table of %d"%num)
for i in range(1,11):
print(num,'*',i,'=',num*i)
output:
enter number: 5
the multiplication table of 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
num=int(input("enter number: "))
print("the multiplication table of %d"%num)
for i in range(1,11):
print(num,'*',i,'=',num*i)
output:
enter number: 5
the multiplication table of 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
Friday, 23 February 2018
command line arguments in python
- the concept of passing the arguments from command line at the time of running the python program/file,that type of arguments are called command-line arguments.
- file name also taken as a one of the command line argument.
- all the command line arguments are stored into list object in the form of strings, that list object stored into the "argv" variable of sys module.
Example:
import sys
print(sys.argv)
print(len(sys.argv))
for p in sys.argv:
print(p)
Output1:
C:\Desktop>python commandlineargument.py hai siva krishna
['commandlineargument.py', 'hai', 'siva', 'krishna']
4
commandlineargument.py
hai
siva
krishna
Output2:
C:\Desktop>python commandlineargument.py arg1 arg2 arg3
['commandlineargument.py', 'arg1', 'arg2', 'arg3']
4
commandlineargument.py
arg1
arg2
arg3
Example:
import sys
if len(sys.argv)==3:
try:
x=int(sys.argv[1])
y=int(sys.argv[2])
z=x+y
print(z)
except(ValueError):
print("enter numerical values only")
else:
print("enter two userdefined arguments only")
Output1:
C:\Desktop>python commandlineargument.py 4 5
9
Output2:
C:\Desktop>python commandlineargument1.py 4
enter two userdefined arguments only
Output3:
C:\Desktop>python commandlineargument1.py 4 abc
enter numerical values only
Thursday, 22 February 2018
reduce function in python
reduce( ) support only python 2.x
reduce( ) doesn't support python 3.x
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
For example,
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
calculates ((((1+2)+3)+4)+5).
The left argument - x is the accumulated value and the right argument - y is the update value from the iterable object.
x=[2,4,5,6,3]
y=reduce(lambda i,j:i+j,x)
print(y)
20
reduce( ) doesn't support python 3.x
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.
For example,
reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
calculates ((((1+2)+3)+4)+5).
The left argument - x is the accumulated value and the right argument - y is the update value from the iterable object.
x=[2,4,5,6,3]
y=reduce(lambda i,j:i+j,x)
print(y)
20
Tuesday, 9 January 2018
difference between is and == in python
"is" vs "=="
-------------------------
- "is" expressions evaluate to True if two variables point to the same object.
- "==" evaluates to True if the objects referred to by the variables are equal.
>>> a = [1, 2, 3]>>> b = a >>> a is bTrue>>> a == bTrue >>> c = list(a) >>> a == cTrue>>> a is cFalse
Switch case implementation in python
Example:
def calculator(operator, x, y):
if operator == 'add':
return x + y
elif operator == 'sub':
return x - y
elif operator == 'mul':
return x * y
elif operator == 'div':
return x / y
else:
return None
print(calculator('add',2,3))
print(calculator('div',6,2))
print(calculator('mod',4,2))
Output:
5
3.0
None
Example: by using dict
def calculator(operator, x, y):
return {
'add': lambda: x + y,
'sub': lambda: x - y,
'mul': lambda: x * y,
'div': lambda: x / y,
}.get(operator, lambda: None)()
print(calculator('add',2,3))
print(calculator('div',6,2))
print(calculator('mod',4,2))
Output:
5
3.0
None
def calculator(operator, x, y):
if operator == 'add':
return x + y
elif operator == 'sub':
return x - y
elif operator == 'mul':
return x * y
elif operator == 'div':
return x / y
else:
return None
print(calculator('add',2,3))
print(calculator('div',6,2))
print(calculator('mod',4,2))
Output:
5
3.0
None
Example: by using dict
def calculator(operator, x, y):
return {
'add': lambda: x + y,
'sub': lambda: x - y,
'mul': lambda: x * y,
'div': lambda: x / y,
}.get(operator, lambda: None)()
print(calculator('add',2,3))
print(calculator('div',6,2))
print(calculator('mod',4,2))
Output:
5
3.0
None
Subscribe to:
Posts (Atom)