Input:
- Input asks the reader to interact
Integer:
- int means to treat as an integer
- e.g. int(user_name)
String:
- str means to treat as a string
- e.g. str(2)
str(day_year)
Floats:
- float() defines a float
- Uses decimals
- e.g. pi = 3.14159
temperature = -5.6
float(3.98)
Variables:
- Storing a value to save for later
- Must be defined before it is used
- e.g. x=7
y=“Hello”
- OR
- e.g. dog_name = "Benny"
print(f"My dog's name is {dog_name}. {dog_name} is cute")
- A variable can equal a variable
- e.g. x=y
y=x
- Can also be e.g. user_name= input(“Please enter your name”)
Index:
- Used to find the specific values in a list
- e.g. my_list = [“apple”, “banana”, “cherry”, “orange”]
value = my_list[2]
print(value)
- This will print ‘cherry’ since it is in position 2 on the list
- Also used to find the position of a value in a list
- e.g. my_list = [10, 20, 30, 40]
position = my_list.index(30)
print(position)
- This will print 2 since the value 30 is in position 2 in the list
Lists:
- Start counting from 0 (0 1 2 3…)
- e.g. my_list = [“A”, “B”, 1, 2, 3*5, 12/3.6, [“X”, “Y”], “Z”]
- Can also create an empty list
- e.g. names_list = [ ]
- Taking the length of a list
- e.g. len(my_list)
Adding and Deleting from the list:
- To add something to the list
- This will add ‘A1’ to the end of the list
- e.g. my_list.append(“A1”)
my_list.append(new_item)
- This will add ‘A2’ to the 3rd place in the list
- e.g. my_list[3] = “A2”
- del deletes
- This will delete the 3rd thing in the list
- e.g. del my_list[3]
- Copying a list
- e.g. c = a[ : ]
While Loops:
- While this is true/not true then keep doing this
- define variables first
- e.g. my_password = “123”
my_guess = “999”
- e.g. while(my_guess != my_password):
my_guess = input(“Please enter your password: “)
print(“Goodbye”)
- This code stores the correct password as my_password
- Defines my_guess to random numbers
- While my_guess is not equal to my_password
- Then keep asking the user for the password
- Else just print goodbye
- *Note* the numbers are being treated as strings
For in Loops:
- For item in sequence do something with item
- for x in
- In list (will print each fruit in new line)
- e.g. fruits = [“apple”, “cherry”, “banana”]
for fruit in fruits:
print(fruit)
- With a string (will print each letter in new line)
- e.g. name = “Katie”
for letter in name:
print(letter)
- With range (will print numbers 1-10 in new line)
- e.g. for number in range(1, 11):
print(number)
- Reverse (will print numbers 1-10 in reverse)
- e.g. for number in reversed (range(1, 11)):
print(number)
- Skip count (will skip count by 2s starting from 1 ending at 10)
- e.g. for number in range(1, 11, 2):
print(number)
- To skip a number (skips 13)
- e.g. for x in range(1, 21):
if x == 13:
continue
else:
print(x)
- Take the length of the names list and starting from the last item *bcz of -1s* will
print a greeting to each name
- e.g. for index in range(len(names_list) -1, -1, -1):
name = names_list[index]
print(“Hello “ + name)
If or Else Statements:
- If statements
- If this is true then do this
- Almost always paired with else statements
- e.g. number =5
if number >0:
print(“This number works”)
- Else statements
- e.g. user_input = input(“What is your grade? “)
grade = int(user_input)
if(grade>=50):
if(grade>80):
print(“You got an A”)
if(grade<60):
print(“You barely passed”)
else:
print(“You failed”)
Elif Statements:
- Else if
- Chain multiple if statements
- If this is true do this but if this is true do this but if this…
- e.g. temperature = 25
if temperature > 30:
print(“It’s hot outside”)
elif temperature > 20:
print(“It’s warm outside”)
elif temperature > 10:
print(“Its cool outside”)
else:
print(“Its cold outside”)
Defining a Function:
- def is used to define a function
- Function is greet and defining the function is the def statement
- e.g. def greet(name):
print(“Hello, “ + name)
greet(“Alice”)
- e.g. def create_name (first, last):
first = [Link]()
last = [Link]()
return first + “ “ + last
full_name = create_name(“spongebob”, “sqaurepants”)
print(full_name)
Return:
- Used inside a define function
- Sends result back to where function was called
- e.g. return a / b
Assert:
- Used as debugger/tester
- Tests if condition is true or not
- e.g. assert b != 0
- e.g. assert (activity == “sleep”)
Equal and Not Equal to:
- Equal to ==
- e.g. my_guess == my_password
day == “Sat”
- Not equal to !=
- e.g. my_guess != my_password
And/Or/Not:
- Always set variables first
- And prints ‘True’ if both conditions are true otherwise prints ‘False’
- e.g. x = 5
y = 10
if x > 0 and y > 0:
print(“Both numbers are positive”)
- Or prints ‘True’ if at least one condition is true if both are false it prints ‘False’
- e.g. a = -1
b=3
if a > 0 or b > 0:
print(“At least one number is positive”)
- Not means this is not equal to
- e.g. food = input(“Enter food you like (q to quit): “)
while not food == “q”
print(“You like” + food)
Text Files:
- Open text file
- e.g. my_file = open(“test_file.txt”, “r”)
- a= append (adds more words to text file)
- w= write (delete everything to add more words)
- r= read, (reads whatever is written)
- .replace() = replaces specific phrases with another specific phrase
- e.g. my_file = my_file.replace(‘Apples’, ‘Oranges’)
- e.g. first_line = my_file.readline()
whole_text = my_file.read()
my_file.write(“Text to add”)
- To skip line
- e.g. my_file.write(“/n”)
Set:
- e.g. my_set = {1, 2, 3, “A”, “B”, “C”, “Hello”}
my_set.add(“New”)
my_set.add(5)
print(“Hello” in my_set) #this will print true since “Hello” is in set
Dictionaries:
- e.g. names_to_ages = {‘Alice’: 27, ‘Bob’: 75}
name = input(“Who do you want to know about? “)
if (name in names_to_age):
age = names_to_ages[names]
else:
print(“Invalid person”)
print(name + “is” + str(age) + “yeard old”)
Is a number → .isdecimal() OR Is a letter → .isalpha()