Initial Commit

This commit is contained in:
Samir Bioud 2023-11-05 11:06:34 +00:00
commit 0926cb5f56
4 changed files with 157 additions and 0 deletions

1
README.md Normal file
View file

@ -0,0 +1 @@
# Python Catchup 2023 Supplementary Material

43
code/1_variables.py Normal file
View file

@ -0,0 +1,43 @@
"""
In this example, we will learn how to use variables in python
FYI: The thing in which I am currently writing is called a comment,
this lets us explain our code.
Comments can take this form, or the hashtag form
Terms:
RHS : Right Hand Side
LHS : Left Hand Side
"""
# Creating a new variable
foo = 30
bar = 12
# We can see the values inside of variables by printing them to the terminal
# Python has a special builtin function called 'print' that does this for us
print(foo)
print(bar)
# We assign the result of the expression on the RHS to the name on the LHS
# In this case, the expression on the right is the sum of the variables 'foo'
# and 'bar'
summed = foo + bar
# Printing the sum of foo and bar
# This should output '42' to the screen
print(summed)
# You can also re-assign variables
bar = 39
# This re-assignment does not affect the value of 'summed'
print(summed) # Still 42
# This is because python variables are immediately evaluated on assignment

42
code/2_types.py Normal file
View file

@ -0,0 +1,42 @@
"""
In this example, we learn about the basic types available in python
"""
# An integer is any whole number
integer_var = 23
# A float is any mathematical rational value
# But also has the ability to represent special values
# such as NaN, Inf, and -Inf
float_var = 12.47
string_var = "Skynet is Alive"
# A Boolean is either True or False
# and you can logical ANDs, ORs, NOTs, etc. on them
boolean_var = True
# A list is, as the name suggessts, a list of data
# in python, the elements of a list can have different types
# as demonstrated below
list_var = [42, 3.14159, "Heehee", False]
# A Dictionary is a mapping of keys into values
# These keys can be integers or strings
# And the values can be just about anything
dict_var = {
"Jan" : "January",
"Feb" : "February",
"Mar" : "March",
"Apr" : "April",
"May" : "May",
"Jun" : "June",
"Jul" : "July",
"Aug" : "August",
"Sep" : "September",
"Oct" : "October",
"Nov" : "November",
"Dec" : "December"
}

71
code/3_type_operations.py Normal file
View file

@ -0,0 +1,71 @@
"""
In this example, we will cover the operations that one can do on each of the primitive types
"""
"""
On Integers, we can do all arithmetic operations
The result type of a binary operation on two integers is an integer
One consequence of this, is that the division operator will implicitly floor the result because integers cannot represent decimal numbers
On a division by 0, python will create a ZeroDivisionError, we will cover those later
"""
number1 = 11
number2 = 12
sum_var = number1 + number2 # 13
dif_var = number1 - number2 # -1
product_var = number1 * number2 # 132
quotient_var = number1 / number2 # 0 (Floor of 11/12)
"""
On Floats, we have the exact same operations available as we do on integers
The result type of a binary operation on two floats is a float
Because of this, division has the proper expected behavior with floats
But prepare for a surprise!!
Try printing out the sum_var after it is assigned, what value do you get?
It should be 0.3, right?
But you are getting 0.30000...0004,How Strange!
This is actually a result of how computers store floating-point numbers
You know the way some numbers for us, such as 1/3 have a reccuring decimal place?
In different numbering systems (bases), the numbers which require an infinite number of decimal places to represent changes
In base 2 (binary), 3/10 needs infinite "decimals" to represent, since you cannot write 3/10 as the finite summation of 2^-x
It would require infinite decimal places, i.e infinite memory to represent, which is not possible
so all we can do is approximate for it!
Look up IEE 754 for more info https://en.wikipedia.org/wiki/IEEE_754
"""
float1 = 0.1
float2 = 0.2
sum_var = float1 + float2 # 0.3
dif_var = float1 - float2 # -0.1
product_var = float1 * float2 # 0.02
quotient_var = float1 / float2 # 0.5
"""
With Booleans, we can use all the logic operators that you covered in Discrete Math
We can do boolean operators using their names, or their symbols
and &&
or ||
not !
xor ^ The xor keyword does not work
"""
bool1 = True
bool2 = True
and_var = bool1 and bool2 # False
or_var = bool1 or bool2 # True
not_var = not bool1 # False
xor_var = bool1 ^ bool2 # True