Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Class 5

1. The document discusses conditional statements and looping statements in Python. Conditional statements like if, elif, and else are used to execute code based on different conditions. Looping statements like for and while loops are used to repeatedly execute code. 2. Some key examples of conditional statements shown include finding the biggest and smallest number from three variables, and printing output based on a condition. 3. Looping statements like for and while are used to repeat code execution. The for loop iterates over a sequence like a range of numbers, while the while loop repeats as long as a condition is true. Examples include printing numbers in a range and adding all natural numbers from 1 to 10.

Uploaded by

Tarique Ejaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Class 5

1. The document discusses conditional statements and looping statements in Python. Conditional statements like if, elif, and else are used to execute code based on different conditions. Looping statements like for and while loops are used to repeatedly execute code. 2. Some key examples of conditional statements shown include finding the biggest and smallest number from three variables, and printing output based on a condition. 3. Looping statements like for and while are used to repeat code execution. The for loop iterates over a sequence like a range of numbers, while the while loop repeats as long as a condition is true. Examples include printing numbers in a range and adding all natural numbers from 1 to 10.

Uploaded by

Tarique Ejaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

+++++++++++++++++++++++

Class 5 notes
+++++++++++++++++++++++

1. Conditional Statements
2. Looping Statements

** Conditional Statements **
****************************

Indentation
-----------
In Python {} are not available to define the body of conditional statement, functions, loop etc.
For define body we use 'indentation'

Syntax:
-------
if <condition>:
#statement
else:
#statement

Variable: a,b=10,20
if a<b:
print('a is less than b')
print('%d is less than %d' % (a, b))
else:
print('a is greater than b')
print('%d is greater than %d' % (a, b))

if <condition>:
#statement
elif <condition>:
#statement
else:
#statement

Get biggest number


-------------------
Variable: a,b,c=10,20,30
if a>b and a>c:
print('a %d is biggest number' % a)
elif b>a and b>c:
print('b %d is biggest number' % b)
else:
print('c %d is biggest number' % c)

Get smallest number


-------------------
Variable: a,b,c=10,20,30
if a<b and a<c:
print('(a=%d) is smallest number' % a)
elif b<a and b<c:
print('(b=%d) is smallest number' % b)
else:
print('(c=%d) is smallest number' % c)

** Looping Statements **
************************
A loop statement allows us to execute a statement or group of statements multiple times.
Repitition work we do using loop.

Python has 2 loop commands:


1. for loop
2. while loop

So before starting loop we should understand about range() function in Python

The range() function returns a sequence of numbers, starting from 0 by default,


and increments by 1 (by default), and ends at a specified number.

range() syntax
--------------
range(start,stop,step)

Some example:
-------------
for x in range(10):
print(x)

for x in range(1,11):
print(x, end=" ")

for x in range(1,20,2):
print(x, end=" ")

for loop
--------
1) Print 1 to 10
for x in range(1,11):
print(x)

2) Print 2 table using for loop


for x in range(1,11):
print(x*2)

3)
1
22
333
4444
55555

For this we will use nested loop


--------------------------------
for row in range(1,6):
for col in range(0,row):
print(row, end=" ")
print("")

4)
1
12
123
1234
12345

For this we will use nested loop


--------------------------------
for row in range(1,6):
for col in range(1,row+1):
print(col, end=" ")
print("")

4)
5
44
333
2222
11111

start = 6
for row in range(1,6):
for col in range(0,row):
print(start-row, end=" ")
print()

5)
11111
2222
333
44
5

for row in range(1,6):


for col in range(row,6):
print(row, end=" ")
print()
months = ["Jan","Feb","Mar","April","May","June"]
for m in (months):
print m

months = ["Jan","Feb","Mar","April","May","June"]
for key, value in enumerate (months):
print(key,value)

while Loop
----------
Using while loop we can execute a set of statements till condition is true.

Syntax:
-------
while <condition or expression>:
#body of loop

i=1
while i < 6:
print(i)
i += 1

# Write a program to add natural numbers from 1 to 10


# sum = 1+2+3+...+10

# To take input from the user


# n = int(input("Enter n: "))

n = 10
# initialize sum and counter
sum = 0
i=1

while i <= n:
sum = sum + i
i = i+1 # update counter

# print the sum


print("The sum is", sum)

while loop with else statment


-----------------------------
count = 0
while count < 5:
print(count," is less than 5")
count = count + 1
else:
print(count, " is not less than 5")

You might also like