Python

Python

Colt Stelle: Notes
Variables
A variable in Python is like a variable in mathematics: it is a named symbol that holds a value.
x = 100 khaleesi_mother_of_dragons = 1 print(khaleesi_mother_of_dragons + x) 101 all, at, once = 5, 10, 15
Naming Restrictions
notion image
notion image
notion image
notion image

Python Escape Sequences

notion image
Examples # Set the message variable equal to any string containing a new-line escape sequence message = "Hello \n goodbye" # Add a string to the mountains variable that when printed results in: /\/\/\ # You will need to use an escape sequence more than once! mountains = "/\\/\\/\\" # Set the quotation variable to any string that contains an escaped double quotation mark quotation = "My cat said \"meow\""
notion image
notion image
notion image
notion image
notion image
notion image
notion image
notion image

for loops

In Python, for loops are written like this:
for item in iterable_object: # do something with item
  • An iterable object is some kind of collection of items, for instance: a list of numbers, a string of characters, a range, etc.
  • item is a new variable that can be called whatever you want

for loops with ranges

Let's print numbers 1 - 7 using our knowledge of looping through ranges.
for number in range(1, 8): print(number)

while loops

This loop continue to execute while a certain condition is truthy, and will end when they become falsy.
user_response = None while user_response != "please": user_response = input("Ah ah ah, you didn't say the magic word: ")
Example: Continues to ask for user input until the user types 'bananas'?
msg = input("whats the secret password?") while msg != "bananas": print("WRONG!") msg = input("whats the secret password?") print("CORRECT!") # for num in range(1,11): # print(num) # equivalent of above for loop num = 1 while num < 11: print(num) num += 1 # equivalent of above for loop num = 1 while num < 11: print(num) num += 1
Example: Print the following beautiful art using both a for loop and a while loop:
šŸ˜€ šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€ šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€šŸ˜€
# For Loop for num in range (1, 11): print("\U0001f600" * num) #While Loop num = 1 while num < 11: print("\U0001f600" * num) num += 1
Example : The"Colt talking to his sister"
Hey how's it going? pretty good, you? pretty good, you? hahah hahah ok very funny ok very funny stop copying me UGH FINE YOU WIN # repeat everything until the user says "stop copying me"
question = input("Hey how's it going? ") while question != "stop copying me": print (question) question = input() print("UGH FINE YOU WIN")
Example: Guess a number between 1 and 10.
import random ri = random.randint(1, 10) attempts = 5 while attempts > 0: num = int(input('Guess a number between 1 and 10: ')) if ri > num: print('Choose a higher number.') elif ri < num: print('Choose a lower number.') else: print('Congratulations! You guessed the right number.') break attempts -= 1 print(f'You have {attempts} attempts left.') if attempts == 0: print(f'Sorry, you have no more attempts left. The correct number was {ri}.')
Ā