Created
July 31, 2019 13:23
-
-
Save sojohnnysaid/f8b63f78ebe2199e6c93d79f151eee91 to your computer and use it in GitHub Desktop.
python basics 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("\n## We'll run main.py ##\n") | |
# using the terminal command python 'program name' | |
# in this case 'python main.py' | |
# we don't use printf anymore instead: | |
print("hello, world") | |
# notice we don't need the ';' | |
# it is not needed anymore in python | |
print("\n## function calls do not require the data types ##\n") | |
# and function definitions use the def keyword | |
def myfunction(): | |
print("hello myfunction") | |
x = 1 | |
print(x) | |
y = "hello my string variable" | |
print(y) | |
myfunction() | |
# notice we don't need '{}' for our function anymore | |
# just a colon ':' | |
# we also do not specify the type being returned | |
# everything inside 'myfunction' is indented 4 spaces | |
print("\n## using the keyword 'True' gives us an infinite loop ##\n") | |
while True: | |
print("hello forever loop") | |
break | |
# however using break it will only run once | |
print("\n## looping something a certain amount of times can be done ##\n") | |
# using the 'range()' function | |
for i in range(10): | |
if i < 8: | |
print(i) | |
elif i == 8: | |
print(i) | |
else: | |
print("9...done with our range!") | |
# also notice we can write if statments without '()' | |
# which is kind of weird at first but less typing! | |
print("\n## data types in python ##\n") | |
# bool, float, int, and string | |
# we can use the typeof() function and print() together | |
# to check types | |
my_bool = type(True) | |
my_float = type(3.14) | |
my_int = type(42) | |
my_string = type("hello string") | |
print(my_bool) | |
print(my_float) | |
print(my_int) | |
print(my_string) | |
# cs50 gives us some helper functions in a library | |
# similar to using #include in C | |
# we'll touch on those when we use them in a problem set | |
# they will allow us to 'get' each of the types | |
# we just mentioned above | |
print("\n## There are other types built into python ##\n") | |
# so you don't have to implement them from scratch | |
# here are the ones mentioned: | |
print("\n## complex ##\n") | |
# The python reference just tells me to ignore this type | |
# if I have no idea what it is used for. I have no idea how | |
# this works but this function has something to get you started. | |
# maybe more for engineers and mathematicians | |
print(complex(1, 2)) | |
print("\n## dict (dictionary) ##\n") | |
# pythons version of a hash table | |
# nothing super complex this is a key/value store | |
# keep in mind the key should always be unique | |
my_dictionary = {"red": 57, "blue": 42, 9: "foo"} | |
for key in my_dictionary: | |
print("key:",key) | |
print(my_dictionary) | |
print(my_dictionary["blue"]) | |
print(my_dictionary[9]) | |
print("\n## list ##\n") | |
# pythons version of an array | |
# remember it is zero base indexed | |
my_list = [1,2,3,4,5] | |
print(my_list) | |
print(my_list[0]) | |
print("\n## range ##\n") | |
# documentation online states this is | |
# "a list of arithmetic progressions." | |
# not actually a list because it is not | |
# a concrete sequence. You call each index on | |
# demand. Use the list() function | |
# to store each value into a list | |
# it is kind of confusing until you use it | |
# range() can take up to three arguments | |
# one argument: just the number to stop at | |
# not including itself (it will start at zero) | |
# return a list up to but not including 10 | |
myRange = list(range(10)) | |
print(myRange) | |
print("\nin a loop the range function will give us each value\n") | |
# on demand. Meaning not in a list stored in memory | |
for i in range(3): | |
print(i) | |
# two arguments: the starting number which is included, | |
# the stop number which is not included in the results | |
myRange = range(1, 11) | |
myRangeList = list(myRange) | |
print("\n", myRangeList) | |
# three arguments: start, stop, step | |
# using step allows us to move the sequence | |
# in multiples of any number within the range | |
myRange = range(0, 21, 4) | |
myRangeList = list(myRange) | |
print("\n", myRangeList) | |
myRange = range(0, 21, 2) | |
myRangeList = list(myRange) | |
print("\n", myRangeList) | |
#### set | |
print("\n## set ##\n") | |
# mutable (able to change) unordered collections of unique elements | |
# you have to have unique elements | |
# you have to run the set() function | |
# that's because the function takes each item | |
# and creates a hash table using a hashing function | |
mySet = set(["boo", "baz", "Mississippi"]) | |
print(mySet) | |
# we add and remove using the add and remove | |
# the add() method works when adding a single element to the set | |
mySet.add("foo") | |
print(mySet) | |
# the remove() method raises an error | |
# if the item is not found which stops | |
# the program | |
mySet.remove("Mississippi") | |
# the discard() method does not throw any errors! | |
mySet.discard("nope") | |
print(mySet) | |
# a common use for a set is to take a list with duplicates | |
# and create a set of only unique elements | |
# very handy! | |
myList = ["zip", "boing", "thwap", "thwap", "bam", "bam"] | |
print(myList) | |
mySet = set(myList) | |
print(mySet) | |
#### tuple | |
print("\n## tuple ##\n") | |
# a tuple is like a list but immutable | |
# a list can change after being created | |
print("a tuple can not change after being created") | |
my_data = ("zero", "one", "two") | |
print(my_data[1]) | |
# tuple can be rewritten but not modified example mydate[0] = "nope" | |
my_data = ("tomato", "bacon") | |
# looping through a tuple | |
for x in my_data: | |
print (x) | |
if "bacon" in my_data: | |
print("true") | |
print(len(my_data)) | |
# tuple can be deleted but not modified | |
del my_data | |
# you can also create a tuple() using the tuple() constructor | |
my_data = tuple(("apple", "banana", "cherry")) | |
print(my_data) | |
# packing is a little weird... | |
# if we have a bunch of variables we can | |
# reassign them in bulk using packing and unpacking | |
# so we are "packing" a bunch of variables into a tuple | |
# and "unpacking" into the tuple of values on the other side | |
# now each one is assigned to each value on the other side | |
# both sides must have the same number of values | |
v1 = 'x' | |
v2 = 'y' | |
v3 = 'z' | |
# reassigning each value | |
(v1, v2, v3) = my_data | |
print(v1) | |
# we can do this with 2 brand new tuples | |
# a tuple of empty variables and a tuples of values to assign | |
# we don't need parenthesis when we do this | |
a, b, c = 'foo', 'baz', 'bar' | |
print(a) | |
# swapping values is one case where this is very useful | |
# because we don't need a temp variable anymore | |
# if we want the above variables a and b to swap we just | |
# pack and unpack them swapped | |
# this is actually pretty cool | |
a, b = b, a | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment