Python Code Flashcards
How to print Hello to terminal?
print(“Hello”)
How to create variable x and then assign value 10 or maybe Hello?
x = 10
x = “Hello”
What symbol is called the assignment operator?
- =
What is the difference between :
1. d = 1 + 4
2. d = “Hello” + “ World”
- 5
- Hello World
- in string is called concatenation
How to know what type value is it in python?
e = 1
y = “Hello”
z = 42.0
type(e)
<type ‘str’>
type(y)
<type ‘int’>
type(z)
<type ‘float’>
How to change string 123 toint?
e = “123”
e_int = int(e)
- Using int() , str() or float()
How to input from user?
name = input(“Name : “)
print(name)
List out 2 methods for commenting
- # ( Single-line Comment )
- ”””””” ( Multi-line Comment )
How to print Hi using multiple 5?
print ( “Hi” * 5 )
What code do we need to put semicolons ?
- if, elif , else
- for, while
- try, except
- def ( functions )
- File Handling ( with open (“g.txt” , “r”) as f : )
- Class Definitions
List out all relational operators
- < Less than
- > Greater than
- == Equal to
- <= Less than or equal to
- > = Greater than or equal to
- != or <> Not equal to
List out all logical operators
- && ( And )
- || ( Or )
- ! ( Not )
Write a program to display the below conditions
1. Below 18 - Not Ok
2. Above 18 & Below 60 - OK
3. Above 60 - Old
age = int(input(“Age : “))
if ( age < 18 ):
print(“Not Ok”)
elif ( age >= 18 ) and ( age < 60 ):
print(“Ok”)
else:
print(“Old”)
How to receive only int input and print out error message when entering wrong values?
try:
age = int(input(“Age : “)
print(“Your age : “ , age )
except ValueError:
print(“Please enter integer values only!”)
List out all the loops type ( 2 )
- While Loops
- For Loops
How to use loop to display integers from 10 to 30?
i = 10
while i <= 30:
print(i)
i += 1
print(“End”)
for i in range(10,31):
print(i)
How to loop using Lists?
pokemon = [“Pikachu”,”Zacian”,”Gengar”]
for i in pokemon:
print( i , “had been registered to pokedex”)
- If it was print( pokemon , “had been registered to pokedex”), it will have below input for 3 times
[‘Pikachu’, ‘Zacian’, ‘Gengar’] had been registered to pokedex
How to sum inside a loop?
total = 0
for i in [9 , 41, 12, 3, 74, 15]:
print(f”Total = {total} + {i}”)
total = total + i
print(total)
How to find the average in the loop?
total = 0
count = 0
for i in [9 , 41, 12, 3, 74, 15]:
total = total + i
count += 1
print(f”Average = {total} / {count}”)
average = total / count
print(f”Average = {average}”)
How to filter the loop, given a list [9 , 41, 12, 3, 74, 15] which requires to print values that is greater than 20?
for i in [9 , 41, 12, 3, 74, 15]:
if i > 20:
print(i)