Python notes
Python notes
2: Variables
First_name = “Bro”
#Boolean
is_food = True
if is_food:
print (“It’s a hamburger”)
else:
print (“Get out”)
#Float
price = 10.99
name = (“Huong”)
age = 16
gpa = 4.5
is_student = True
print(type(name))
For Ex:
Age = str(age)
age += “1”
print (age)
—> 251
1.4 user input - function that prompts the user to enter data (Returns the entered data as a
string)
print (area)
friends = 0
#friends = friends + 1
friends +=1
print (friends)
—> 1
remainder = friends %2
→0
————————————
x = 3.14
y= -4
z=5
result = round (x)
print (result)
—> 3
import math
print (math.pi)
result = math.sqrt(x)
result = math.ceil (x)
result = math.floor (x)
1.7 if Statements
If
elif
else
else:
print(“No food for you!”)
temp = 25
is_raining = False
Ex.2:
a=6
b=7
max_num = a if a > b else b
min_num = a if a < b else b
print (min_num)
—> 6
Ex. 2
phone_number (“Enter your phone #”)
phone_number = phone_number.replace(“-“, “ “)
print (phone_number)
print (help(str))
credit_number = “1234-5678-9012”
print (credit_number[4)
—> -
print (credit_number[0:4])
—> 1234
print (credit_number[5:])
—>5678-9012
credit [::2]
—> 13-68-02
#format specifiers = {value:flags} format a value based on what flags are inserted
Loops:
for x in range (1,21):
if x==1:
break
else:
print(x)
Nested Loops
#nested loop = A loop within another loop (outer,inner)
#outer loop:
#inner loop:
for x in range(1,10): #10 is exclusive
print(x,end="")
→ 23456789