Varibles Naming Rules: Must start with a letter or underscore _
x, y, z =12.2,14,'Python'# the right hand side is actually a tuple. We will learn tuples at a later time.print(x, y, z)
Arithmetic Operators:
**
power
/
division
%
remiander
//
quotient
hashtag#
About the is operator
is checks whether both the operands refer to the same object or not, i.e., the same id.
== compares the values only.
a,b=2,2
a is b
>>False
a == b
>>True
Conditionals
nested conditionals
if...:if...:
chained conditionals
if...:elif...:else:
try/except structure
inp =input('Enter Fahrenheit Temperature: ')try:
fahr =float(inp)
cel =(fahr -32.0)*5.0/9.0print(cel)except:print('Please enter a number')>>Enter Fahrenheit Temperature: MSBA
>>Please enter a number
Functions
routine
deffuc(a,b):return a+b
lambda expression one-line; anonymous can difine a variable by condition to go on!!
addone =lambda x: x+1
sumup =lambda x, y: x+y
sumup(1,2)>>3# conditional output
compare =lambda x: x*2if x <10else x
compare(12)>>12
compare(5)>>10# directly execute the lambda function
v =(lambda x: x * x)(100)
v
>>10000#conbined fuc&lambda
my_list =[12,65,54,39,102,339,221,50,70]
result =filter(lambda x:(x %13==0), my_list)print(list(result))
map function
# map is used to let one function to run in mutiple objectsabs(-3)>>3abs(-3,-5,-7)>> error
list(map(abs,(-2,10,-9)))>>[2,10,9]# map & def fucdefaddition(n):return n + n
numbers =(1,2,3,4)
result =map(addition, numbers)print(list(result))>>[2,4,6,8]#map & lambda
num1 =[1,2,3]
num2 =[4,5,6]
result =map(lambda x, y: x + y, num1, num2)print(list(result))>>[5,7,9]
Loops and Iteration
continue and break
whileTrue:
line =input('>>>Enter something: ')if line[0]=='#':continue# ends the current iteration # jumps to the top of the loop# starts the next iterationif line =='done':break# jumps out of the entire loopprint(line)print('The End!')>>>Enter something: hku
hku
>>>Enter something:# hello>>>Enter something: ???
???
>>>Enter something: done
The End!
while vs for while means condition is met; for means iteration variable
nested for loops
# range print direction —— default:lengthwaysfor x inrange(5):for _ inrange(x +1):# we use _ as the interation variable# we do not need to use its valueprint('*', end ='')print()>>***************