Tutorials
Week 1 - Basics
# This is a comment you can write any thing you want here
# Name: James Shima
# Purpose of this python program: show some basic examples
"""
This is a multi line comment
I can write comments on multiple lines without using a #
everytime!
"""
# if you want to comment out a peace of your code (program),
# highlight it, hold CONTROL and also press / to comment it out and repeat to undo
# to undo a change(s) hold CONTROL and press z
# ================ TYPES ===================
x = 5 # this is a INTEGER aka a whole number (we will call it an Integer in this class)
x = 5.1 # this is a FLOAT aka a number with a decimal place
x = "Hello" # this is a STRING, which is data that holds letters or keyboard CHARACTERS as they appear in the quotes
# You can tell this is a string due to the quotes "" around it
# you can also use '' single quotes to make a string but I like the double quotes for a string and '' for a string with just
# one letter aka a CHAR
x = 'A' # this is a STRING as well but more formally called a CHAR. A string is a group/multiple CHARs or keyboard characters
x = [] # this is a LIST, it can hold multiple variables/types or items for you. This one is empty
x = [5, 5.1, "Hello", 'A', 0, 3.14] # they are usefull for storing groups of things in one place
5 # what does this do?
# NOTHING! its just a 5 floating in empty space within our program without a variable to be assigned to xd
# this is an example of a SEMANTIC ERROR (an error that works but doesn't do what we want)
x = float("infinity") # trick if you need infinity
x = input("Enter your name> ") # this is the input function, it takes input from the user and stores it in x AS A STRING!!!!
# the string "Enter your name> " is a custom prompt for the user to see what to input, it is also generally called
# a parameter, more on this and functions later...
print("Hello World") # this is the print function, it takes in anything and outputs it to the user to see
print(x) # printing a variable
print(f"The user put in {x}") # this is print with an f string. f strings allow you to put variables or expresions
# inside a string for example f"The value of 2+2 is {2+2}" --> The value of 2+2 is 4
print(f"The value of 2+2 is {2+2}") # --> The value of 2+2 is 4
x = "3.3" # is this a float or a string?
x = float("3.3") # what about now
x = "5" # is x a string or an int?
x = int("5") # what about now?
# ever unsure of what type something is?
print(type(x))
x = x + 1 # How is this possible???
# = does not mean equals! it means assign a new value to the left hand side.
# so this is assigning x to be what x is currently, but with one added to it
# so if x is initally 5, then x = x+1, would make x now be 6 as x = x-->(5) + 1 is 6
# WE WILL USE THIS TRICK A LOT SO DON'T MIX THIS UP
# what's e? SCIENTIFIC NOTATION!
x = 1e3 # scientific notation (USEFUL for your first assignment)
x = 5.2e11 # 520000000000.0 lot nicer than writing all those zeros!
# 1e3 means 1 * 10^3 = 1000
# CAREFUL this give you a float and not an int so use the x = int(x) statement if needed
x = x % 2 # Gives us the remainder when dividing by 2
# (think back to 3rd grade long division and you did R for the remainder)
# this is the R value! It is useful for solving many problems
x = x // 2 # division but chop off any decimal point (gives us a integer without rounding)
"""
Variable Names:
- Must not start with a number (but can after the first character)
- No spaces!
- Should be specific (not a or b or c... etc.)
- Seperate words with an underscore i.e. (variable_with_long_name) this is called Snake Case
- No dashes! or other special symbols except _
- No words reserved for python (sum max min count int print ...) are a few No Nos
"""
Week 2 - Lists, Strings, and Binary Oh My!
f-strings
f strings or "format" strings are a nice way to print your output without using commas
x = 5
print(f"the value of x is {x}")
print(f"x+1 is {x+1}!")
# the value of x is 5
# x+1 is 6
Nice right!?
Rounding
f strings allow you to round floats as well! print(f"round this number to 2 decimal places {2.111111:.2f}")
just add the :.Xf where X is the number of decimal places to add/round. This works even if its zero it wil add X amount of zeros to the end.
String Magic
x = "hello"
y = " world!"
print(x+y)
# hello world!
print(x*5)
# hellohellohellohellohello
print(f'{(x+" ")*5}') # notice I had to change "" and '' in an f string!
# hello hello hello hello hello
String Methods
the .split() method will seperate a string into elements of a list seperated by whitespace(by default) i.e(spaces newlines tabs) or by a common seperator between the strings that is provided. The .join() method will take in a list and do the opposite of split joining each element of the list by the String example
s = "hello world!"
s = s.split()
print(s)
# ["hello", "world!"]
s = " ".join(s)
print(s)
# hello world!
s = "hello@$!there@$!mate"
s = s.split("@$!")
print(s)
# ["hello", "there", "mate"]
s = "@".join(["my","dog","tommy"])
print(f"{s}")
# my@dog@tommy
Lists and List Methods
- sorted() sorts a list
- map() applies a function to each list member
- max() takes the max value from the list
- min() takes the minimum value from the list
- .index() takes in an element and returns the first index of its location
- .append() adds an element to the end of a list
- .pop() removes an element from the end of a list
- .remove() removes all element from the the list
- list() creates a new list/converts something into a new list
- [] creates a new empty list i.e. l = []
l = []
l.append(1)
l.append(3)
l.append(2)
print(l)
# [1, 3, 2]
l = sorted(l)
print(l)
# [1, 2, 3]
l = list(map(str, l))
print(l)
# ["1", "2", "3"]
l = list(map(int, l)) # convert each element back to int
print(f"MAX:{max(l)}, MIN:{min(l)}")
# MAX:3, MIN:1
l.pop()
print(l)
# [1, 2]
l.index(1)
# 0
l.index(2)
# 1
l.index("hello")
# Error!
Slicing
uses the [] operator -> [start index : end index(non-inclusive) : optional step]REMEBER THIS:
Reverse a string/list
l = [1,2,3]
l = l[::-1]
print(l)
# [3,2,1]
s = "hello"
print(s[0])
# h
print(s[-1])
# o
print(s[-2])
# l
print(s[1:])
# ello
print(s[2:4])
# ll
print(s[::-1])
# olleh
"""
Everything above applies to lists as well
"""
Mutable vs Immutable
Mutable -> you can change/reassign an element in it! ex: lists, dictionariesImmutable -> you cannot change any of its elements! example: strings, tuples
l = ["H","I"]
l[0] = "h"
# ["h", "I"]
s = "hello"
s[0] = 'H'
# ERROR strings are Immutable!
# you have to make a new entire string to accomplish this!
s = "H" + s[1:]
# or just...
s = "Hello"
print(s)
# Hello
BINARY
Binary is how computers understand instructions or know what to do. Binary is simply a string of 1's and 0's. When python runs under the hood, the computer is actually reading what we tell it to do in this format.
i.e print("hello") -> 010101011110101011110101010101101000001010101110101
Binary is actually a number system with base 2. We humans use a base 10 number system due to us having 10 fingers. i.e 15 in base 10 is just 15 while in binary it is 1111. How this works
is simple. 15 = 1 * 10^1 + 5 * 10^0 for base 10! 15 = 1*2^3 + 1*2^2 + 1*2^1 + 1*2^0 for base 2 (binary). Generally a number in any base can be calculated as. Number = {0...base-1} * base^place + ... + {0...base-1} * base^0. All that to say when our computers do math or calculations, its doing them in base 2 then just converting or showing us the result in base 10! For example 2+2 = 0010 + 0010 = 0100 = 4
print(bin(4)) # "0b100"
the 0b means its a binary string in python, if you want just the numbers you can do bin(4)[2:]