0% found this document useful (0 votes)
11 views

Python Cheat Sheet

Uploaded by

Navin Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Cheat Sheet

Uploaded by

Navin Rai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Floor division will return

float value if one or both


operands are floats.
The in operator

 An operator to check for membership of a sequence. Examples


 c in "aeiou"
 num in range(1, 100)
 Result: True or False
for loop
• Use a for loop when you know how many times you want to repeat a computation
while loop
• Use while when you want to exit a loop when a certain situation arises
• Often not knowing in advance how times you need to loop.
Examples
1. You want to repeatedly read in marks to a class of n students and find the average. Should
you use for loop or while loop?
2. You want to repeatedly read in marks to a class of students but you do not know the number
of students before hand. You will enter a negative mark when there are no more students.
Should you use for loop or while loop?
Example 1: Use for loop
sum = 0
for i in range(n):
marks = int(input("Enter score:"))
sum += marks
print("Average marks: ", sum/n)

Example 2: Use while loop


sum = 0
n=0
mark = int(input("Enter mark: "))
while mark >= 0:
sum += mark
n += 1
mark = int(input("Enter next mark: "))
print("Average mark: ", sum/n)

Example 3:
• Write a loop to evaluate this summation series given n:
n
1
∑ i(i+1)
i=1
sum = 0
for i in range(1, n+1):
sum += 1/(i*(i+1))

Example 4:
• Write a loop to evaluate this summation series to 8 decimal places:

1
∑ i(i+1)
i=1
Summation Series 2
sum = 0
eps = 1e-8
i = 1
term = 1/2 # = 1/(1*(1+1)) for first term
while term > eps:
sum += term
i += 1
term = 1/(i*(i+1))

Nested loops
• The inclusion of a loop inside another

• The nesting can be any combination of for and while


• for – for
• for – while
• while – while
• while – for
n m
• Example: ∑ ∑ ¿ ¿ ¿)
i=1 j=1

• There can be more levels of nesting


n m p
• Question: can you write the nest loop for ∑ ∑ ∏ (i∗j+ k) ?
i=1 j=1 k=1

Summation Series 3 with nested loops

sum = 0

for i in range(1, n+1):

for j in range(1, m+1):

sum += i * j
Infinite loop

• A loop that goes on for ever. Your program doesn’t terminate.

• Usually this is an error.

• Sometimes this is deliberate, but with a condition within the loop that allows breaking
out
Break

• Stops the computation in a loop and forces exit from the loop

• In a nested loop situation, a break statement breaks out of the loop directly containing
it.
Summation Series 4
sum = 0
eps = 1e-8
i = 1
term = 1/2 # = 1/1*(1+1) for first term
while True:
sum += term
i += 1
term = 1/(i*(i+1))
if term < eps:
break

continue

• Stops the computation in a loop and return to the top of the loop

• In a nest loop situation, a continue statement returns to the top of the loop directly
containing it.
for … else (and while … else too)

You might also like