1.
2: Variables
First_name = “Bro”
print (f”Hello {first_name}”)
—> Hello Bro
#Boolean
is_food = True
if is_food:
print (“It’s a hamburger”)
else:
print (“Get out”)
—> It’ s hamburger
*If claim is_food = False, then Get out is the statement shown in terminal
#Float
price = 10.99
1.3 Typecasting: the process of converting a variable
star (), int (), float (), bool ()
name = (“Huong”)
age = 16
gpa = 4.5
is_student = True
print(type(name))
—> <class ‘str’>
For Ex:
age = float (age)
print (age)
—>16.0
gpa = int (gpa)
print (gpa)
—> 4
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)
Name = input(“What is your name?:”)
print (f”Hello {name}!”)
—>What is your name?
Type in name; results: —> Hello, (Name)
# Exercise 1 Rectangle area Calc
length = float(input(“Enter the length: “))
width = float(input(“Enter the width: “))
area = length * width
print (area)
#Exercise 2 Shopping Cart Program
1.5: Write Madlibs game
1.6 Arithmetic & Math
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
result = abs (x)
result = pow (4,3)
result = max (x,y,z)
import math
print (math.pi)
result = math.sqrt(x)
result = math.ceil (x)
result = math.floor (x)
1.7 if Statements
If
elif
else
response = input(“Would you like food? (Y/N): “)
if response == “Y”:
print (“Have some food!”)
else:
print(“No food for you!”)
1.8 Calculator Program, weight converter, logical operators
temp = 25
is_raining = False
if temp > 35 or temp < 0 or is_raining
print (“The outdoor event is cancelled”)
else:
print (“The outdoor event is still scheduled”)
—> The outdoor event is still scheduled
1.9 Conditional expressions
Ex.1:
num = 5
print (“Positive if num > 0 else “Negative”)
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
2.0 String methods
len () = length of a function
name = input (“Enter your full name:”)
result = len(name)
If enters Tom Ho
—> 6
#result = name.find(“o”) - first occurance
#result = name.rfind(“o”) - last occurance
#result = name.capitalize()
#result = name.isdigit() [true if only contains digit]
#result = name.isalpha() [if enters Bro Code, contains space so false)
Ex. 2
phone_number (“Enter your phone #”)
phone_number = phone_number.replace(“-“, “ “)
print (phone_number)
print (help(str))
2.1 String indexing
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
To reverse number, set to be - 1
#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
Lists, sets, and tuples
fruits = ["apple","orange","banana","coconut"]
fruits[0]="pineapple"
for fruit in fruits:
print(fruit)
→
pineapple
orange
banana
coconut