PYTHON BASICS TIPS TO REMEMBER
LETS BUILD A BRIDGE OF LEARNING HABBITS
If else statement
Tips to remember while using python
• always specify numbers as int if we need output • age >=(is greater or equal to 14)
age=input("what is your years: ")
age=int(age)
if age >=14:
print ("your are eligible to play")
else:
print("you are not eligible")
print(age)
nested if else and
Tips to remember while using python• if we need the particular output in quiz
Remember to mention user_input == winning_number)
winning_number = 27
user_input = input("guess a number b/w 1 and 100: ")
user_input = int(user_input)
if user_input == winning_number:
print("you win!!")
else:
if user_input < winning_number:
print("to low")
else:
print("to high")
print(user_input)
and or statement
Tips to remember while using python• if value is greater than age input age>=10, • position specified in list format user[0]==('a') or user[0]==('A')):)
user = input("what is your name: ")
age = input("what is your age: ")
age = int(age)
if age>=10 and (user[0]==('a') or user[0]==('A')):
print("you can watch movie")
else:
print("you cannot watch movie")
if elif else statements
Tips to remember while using python• if input value is equal to 0 & input value is less than 0 age==0 & a<0 ,
• age specific from 0,1,2,3 - 0
for multiple statements
age = input("what is your age: ")
age = int(age)
if age==0 or age<0:
print("you cannot watch movie")
elif 03:
print("free ticket")
elif 310:
print("charge 150")
elif 1060:
print("charge 350")
else:
print("price rs. 500")
if with in
Tips to remember while using python• shortcut to remember when can we use - in)
name = "chirag"
if 'a' in name:
print("a is present")
else:
print("not present")
empty or not
Tips to remember while using python• strange example but very effective while user could input any thing or even can just hit a enter)
name = "chirag"
if name:
print("empty string")
else:
print("not empty")
name = input("enter your name: ")
if name:
print(f"your name is {name}")
else:
print("you din\'t any thing")
and - both statement should be true than only it could be true
Tips to remember while using python• checking 2 conditions are correct or not)
name = "abc"
age = 24
if name=="abc" and age==24:
print("condition true")
else:
print("condition false")
or - - any 1 statement than it can be true
Tips to remember while using python• checking any 1 conditions is correct or not)
name = "abc"
age = 24
if name=="abc" or age==16:
print("condition true")
else:
print("condition false")
WHILE LOOP
Tips to remember while using python
• it is printing every time hello world till the value doesn’t reach the while loop value (I is smaller equal to 10
• how while loop works we assigned i+1 = 2 afterwards it adds +1 till the value 3,4,5,6, 10 comes afterwards it stops)
i = 1
while i<=10:
print("hello world")
i = i+1
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
Tips to remember while using python
• Secret tip Using shortcut always remember, (f"hello world {i}")), in this we are getting the input value also to be printed as we are assigning i+1 value each time till it reaches 10
i = 1
while i<=10:
print(f"hello world {i}")
i = i+1
hello world 1
hello world 2
hello world 3
hello world 4
hello world 5
hello world 6
hello world 7
hello world 8
hello world 9
hello world 10
WHILE LOOP HOW TO SUM
Tips to remember while using python
Trick : we are getting a sum of 2 numbers till we reach at a value 10
It check what values are assigned and as per seeing we see while loop reverses the formula from bottom to the word while writte, but always what we need the output needs to be remembered
total=0
i = 1
while i<=10:
total +=i
i +=1
print (total)
55
Enter your e-mail below and get notified on the latest blog posts.