Professional Documents
Culture Documents
x = 1
print(x)
x_str = str(x)
print("my fav num is " + x_str + ". " + "x = " + x_str)
2
• print() - throws cursur to next line
• print(“string”) - prints string on screen
• We use repetition operator (*) to repeat the strings in
the output as
• print(3 * 'Hi') - prints HiHiHi
• print(variables list)
• a,b = 2,4
• print(a,b) 24
• print(a,b, sep=':') 2:4
print(object) statement
• We can pass objects like lists,tuples or dictinaries to the print function
to display the elements of these objects
• lst = [ 10,'d','hi']
• print(lst) prints [ 10,'d','hi']
>>> name
'Paras Hilton'
INPUT/OUTPUT: input("")
prints whatever is in the quotes
user types in something and hits enter
binds that value to a variable
text = input("Type anything... ")
print(5*text)
8
input
• to read numbers, cast input result to an int or float
• If the user does not type a number, an error occurs.
• Example:
age = int(input("How old are you? "))
print("Your age is", age)
print(65 - age, "years to retirement")
Output:
How old are you? 53
Your age is 53
12 years to retirement
• x=input("Enter First number:")
• y=input("Enter Second Number:")
• i=int(x)
• j=int(y)
• print("The Sum:", i+j)
a,b = 10,20
result = eval(“a+b-10”)
print(result)
20
x = eval(input(“Enter an Expression...”)
print(“result = %d” % x)
Enter an Expression... 20 + 30 * 2
result = 80
Part A: Decision Control Statements
○ If
● If-else
● Nested if
● if-elif-else
Part B: Basic loop:
a. while loop
b. for loop
c. Nested loops
d. The break, continue , pass , else statement
used with loop
Decision Control Statements
Decision control statements usually jumps from one part of the code to another depending
on whether a particular condition is satisfied or not .
That is they allow to execute statements selectively based on certain decisions .
If statement
If-else statement
Nested if else statement
If-elif-else statement
If Statement
Syntax:
If test_Expression :
Statment1
……………
Statement n
Statment x
If Statement
x=5
if(x<10):
print(“x is smaller”)
if(x>20):
print(“x is greater”)
print(“Finish”)
Output : X is Smaller
Finish
Indentation importance
x=5
Print (“before 5”)
if(x==5):
print(“is 5”)
print(“is still 5”)
print(Third 5”)
print(“Afterword 5”)
print(“Before 6”)
if (x==6):
print(“is 6”)
print(“is Still in 6”)
print(“Third 6”)
print(“Afterword 6”)
If-else syntax
if(test condition ):
Statement block 1
else:
Statement block 2
Statement x
If-else statement
x=4
if(x>2):
print(“Bigger”)
else:
print(“Smaller”)
print(“All Done”)
Nested if statement
If statement can be nested that is, can be placed one inside the other .
If statement is the statement part of outer one.
if-elif-else
Note that it is not necessary that every if statement should have else block as
python supports simple if statement also.
After their first test expression or the first if branch , the programmer can
have as many elif branches as he wants depending on the expression that
have to be tested.
A series of if-elif statements can have a final else block.
if-elif-else statement
if(x<2):
print(“small”)
elif(x<10):
print(“Medium”)
else:
print(“Large”)
print(“done”)