diff --git a/2_hello_world/README.md b/2_hello_world/README.md index dbc9581..36c2fa9 100644 --- a/2_hello_world/README.md +++ b/2_hello_world/README.md @@ -85,10 +85,14 @@ len equ $ - msg ;length of our dear string #### Dynamic Typing All programming languages let you allocate memory and save its value to something known as a **variable**. It works the -same way as in math class, the variable can be anything it just must be defined first before its used. Some languages -are *Typed* meaning that you have to explicitly say *"This variable is an Integer"* or *"This variable is a Word"*. -Python is Dynamically Typed, so you don't need to explicitly say what type a variable is. We'll sort this out more in a -few sections. +same way as in math class; the variable can be anything it just must be defined first before its used. + +Some languages are *statically typed*, meaning that once you say define a variable as having a particular type +(e.g. integer or string), whether explicitly or implicitly, they cannot have their types changed. This will usually be +enforced with type checks performed by the compiler. + +Python is *dynamically typed*, so you can change the variable type by simply assigning the desired value. You don't need +to explicitly say what type a variable is in Python, though we'll revisit this idea in the next section. ## Try it Yourself @@ -104,4 +108,4 @@ greeting with your name! Mine would be... print("Hello World") # My new line below print("Hello, Dan!") -``` \ No newline at end of file +``` diff --git a/3_variables/README.md b/3_variables/README.md index be40541..c9263e6 100644 --- a/3_variables/README.md +++ b/3_variables/README.md @@ -11,7 +11,7 @@ and swap out whenever you want. Look at the example below. ```python # File some_number = 7 -print someNumber +print(some_number) # Endfile, output below 7 ``` @@ -32,17 +32,12 @@ capitalize. The right side of the declaration can be any of any type. Each of the types are explained below. -#### Integers & Longs +#### Integers -* Integers - Integers are positive or negative whole numbers with no decimal point. - - Examples: `5, 4, 7, 10, -15, 0, 100, -145` - - Creation: Either you can just put the number like `5` or if you have a string that can be turned into a number, - wrap it in `int()` like `int("5")` will equal `5`. -* Longs - Longs are integers of unlimited size, written like integers and followed by an "L". Use these when a number -is too large for an Integer. - - Examples: `10000000L, -100323434L, 134234245345L` - - Creation: Either you can just put the number like `10000000L` or if you have a string that can be turned into a number, - wrap it in `long()` like `long("100000000")` will equal `100000000L`. +Integers are positive or negative whole numbers with no decimal point. +- Examples: `5, 4, 7, 10, -15, 0, 100, -145` +- Creation: Either you can just put the number like `5` or if you have a string that can be turned into a number, + wrap it in `int()` like `int("5")` will equal `5`. #### String @@ -55,15 +50,15 @@ length zero and be equivalent to `""`. Strings can be single or double quoted. example: `"He said to me, \"Woah\""`. If you don't want to escape it, then you can put `r` infront of it like so ```python -print 'C:\\nowhere' # Will be escaped to print 'C:\nowhere' -print r'C:\\nowhere' # Will print as is. 'C:\\nowhere' +print('C:\\nowhere') # Will be escaped to print 'C:\nowhere' +print(r'C:\\nowhere') # Will print as is. 'C:\\nowhere' ``` Strings can be added together like so: ```python greeting = "Hello, " + "Dan, " + " you look good today!" -print greeting +print(greeting) # End of file, output below Hello, Dan, you look good today! ``` @@ -99,8 +94,8 @@ x <= y # ... x is less than or equal to y # Some examples of boolean expressions age = 18 # Int variable -old_enough_to_get_driving_licence = age >= 16 # True -print(old_enough_to_get_driving_licence) +old_enough_to_get_driving_license = age >= 16 # True +print(old_enough_to_get_driving_license) # Another example number = 7 @@ -147,10 +142,13 @@ Floats usually represent decimal numbers, but they can also represent numbers in ```python # Float examples -example1 = float(6/8) -example2 = float(7/3) +example1 = 6/8 +example2 = 7/3 example3 = 2.323 example4 = example1 / example3 +example5 = float(7) +example6 = float("5") +example7 = float("3.14") ``` @@ -167,51 +165,51 @@ To access values in lists, use the square brackets for slicing along with the in available at that index. ```python - # Index: 0 1 2 3 4 -myNumberList = [1, 4, 6, 8, 10] -myNumber = myNumberList[0] # Get the first number; will be 1. + # Index: 0 1 2 3 4 +my_number_list = [1, 4, 6, 8, 10] +my_number = my_number_list[0] # Get the first number; will be 1. -myStringList = ["Hello", "World"] -myString = myStringList[1] # Equates to "World" +my_string_list = ["Hello", "World"] +my_string = my_string_list[1] # Equates to "World" -myMixedList = [1, "Hi", 15, True] -myMixed = myMixedList[3] # Equates to True +my_mixed_list = [1, "Hi", 15, True] +my_mixed = my_mixed_list[3] # Equates to True ``` To update the value at an index, you can access the value with the square brackets, and then just put that on the left side of a declaration statement. ```python -list = ['physics', 'chemistry', 1997, 2000]; +my_list = ['physics', 'chemistry', 1997, 2000] -print "Value available at index 2 : " -print list[2] -list[2] = 2001; -print "New value available at index 2 : " -print list[2] +print("Value available at index 2 : ") +print(my_list[2]) +my_list[2] = 2001 +print("New value available at index 2 : ") +print(my_list[2]) ``` -To add to an existing list, use `append(some_value)` +To add to an existing list, use `list.append(some_value)` -``` -list = [1, 10, 15] -list.append(5) -print list # Will print "[1, 10, 15, 5]" +```python +my_list = [1, 10, 15] +my_list.append(5) +print(my_list) # Will print "[1, 10, 15, 5]" ``` -To remove from an existing list, use `remove(some_value)` +To remove from an existing list, use `list.remove(some_value)` -``` -list = [1, 10, 15] -list.remove(10) -print list # Will print "[1, 15]" +```python +my_list = [1, 10, 15] +my_list.remove(10) +print(my_list) # Will print "[1, 15]" ``` -If you want the value returned back, use `pop(index)` -``` -list = [1, 10, 15] -my_num = list.pop(0) -print myNum # Will print "1" +If you want the value returned back, use `list.pop(index)` +```python +my_list = [1, 10, 15] +my_num = my_list.pop(0) +print(my_num) # Will print "1" ``` * You can mess around and assign different variables in `variables.py` and see how they change! @@ -222,10 +220,9 @@ Dictionaries are like arrays, but allow you to map a key to a value. With lists, ```python my_dictionary = {"Dan": 19, None: "Nothing", 5: "My age.", "coolness": False} -print my_dictionary["Dan"] # Prints 19 +print(my_dictionary["Dan"]) # Prints 19 my_dictionary["new_key"] = "My New Value" -print my_dictionary # Prints '{"Dan": 19, None: "Nothing", 5: "My age.", "coolness": False, "new_key": "My New Value"}' +print(my_dictionary) # Prints '{"Dan": 19, None: "Nothing", 5: "My age.", "coolness": False, "new_key": "My New Value"}' ``` - diff --git a/4_loops_and_conditionals/README.md b/4_loops_and_conditionals/README.md index 834527d..96d3a32 100644 --- a/4_loops_and_conditionals/README.md +++ b/4_loops_and_conditionals/README.md @@ -141,7 +141,7 @@ code_outside_of_loop # We're on time 1 # We're on time 2 for x in range(0, 3): - print "We're on time %d" % (x) + print("We're on time %d" % x) # Iterate through a list @@ -168,9 +168,9 @@ Take the codeblock below, when a boolean expression is passed to the `if` statem ```python n = random.randInt(-10, 11) # Pick a random number between -10 and 10. if n < 0: - print ("The absolute value of",n,"is",-n) + print("The absolute value of",n,"is",-n) else: - print ("The absolute value of",n,"is",n) + print("The absolute value of",n,"is",n) ``` and that has the output of ``` @@ -181,7 +181,7 @@ Integer? 1 The absolute value of 1 is 1 ``` So what is going on? Python reads the line "if n < 0:". If n is less than zero Python runs the line -`print("The absolute value of",n,"is",-n)`. Otherwise python runs the line `print("The absolute value of",n,"is",n)`. +`print("The absolute value of", n, "is", -n)`. Otherwise Python runs the line `print("The absolute value of", n, "is", n)`. More formally, Python looks at whether the expression `n < 0` is **True** or **False**. An if statement is followed by an indented block of statements that are run when the expression is `True`. After the if statement is an optional @@ -198,11 +198,11 @@ a = 0 while a < 10: a = a + 1 if a > 5: - print (a,">",5) + print(a, ">", 5) elif a <= 7: - print (a,"<=",7) + print(a, "<=", 7) else: - print ("Neither test was true") + print("Neither test was true") ``` With the output - Notice how the elif a <= 7 is only tested when the if statement fails to be true. elif allows multiple tests to be done in a single if statement: diff --git a/5_functions_and_files/README.md b/5_functions_and_files/README.md index eda8d60..75a91ba 100644 --- a/5_functions_and_files/README.md +++ b/5_functions_and_files/README.md @@ -38,7 +38,7 @@ You can define functions to provide the required functionality. Here are simple An example of a function would be: ```python -def print_oddly(myString): +def print_oddly(my_string): """ The function prints each letter of a string on a new line. myString: String variable of any size @@ -48,18 +48,18 @@ def print_oddly(myString): print("I am outside the function!") -myName = "Dan Starner" +my_name = "Dan Starner" -print_oddly(myName) +print_oddly(my_name) ``` * We define the function on the first line with the `def` keyword, followed by the name we want to give it - in this case `print_oddly`. -* In the parenthesis, we list what variables we want to be passed when the function is called. Here it is `myString`. +* In the parenthesis, we list what variables we want to be passed when the function is called. Here it is `my_string`. * We then declare the function docstring within the `"""`s. The docstring tells other programmers and readers what the function does. -* Inside the function and indented 4 spaces, we place the codeblock that we want to run. In this case it is the `for` loop, that prints out each letter of `myString` on a separate line. Note how everything is indented 4 spaces PLUS what it normally would be. +* Inside the function and indented 4 spaces, we place the codeblock that we want to run. In this case it is the `for` loop, that prints out each letter of `my_string` on a separate line. Note how everything is indented 4 spaces PLUS what it normally would be. * The `print()` statement at the bottom is NOT a part of the function. When the indenting stops, the function stops kind of like a conditional does. -* Below the first `print()` statement, we define a variable `myName` and set it to a string. -* On the last line of the code, we call the function with its name, parentheseses, and any arguments we have to pass it. When it hits this line, it will call the code within `print_oddly` with myString being set to the value of `myName`. +* Below the first `print()` statement, we define a variable `my_name` and set it to a string. +* On the last line of the code, we call the function with its name, parentheseses, and any arguments we have to pass it. When it hits this line, it will call the code within `print_oddly` with `my_string` being set to the value of `my_name`. Functions are **VERY** cool, because it should stop you from having to write the same line of code more than once. Let's say you have to write out tons of words with the letters on different lines - why you would have to do this I have no idea - diff --git a/6_classes/README.md b/6_classes/README.md index 76360fd..bee3cbc 100644 --- a/6_classes/README.md +++ b/6_classes/README.md @@ -13,11 +13,11 @@ already seen - integers, booleans, strings... - but there are also variables tha that can contain as much information as desired. Its a way to more easily represent the world around us. Think of any object, for this we will use a car. We can identity as few or as many ways to describe the car. -* Brand - "Jeep" -* Make - "Liberty" -* Year - 2006 -* Owner - "Daniel S" -* # of Wheels - 4 --> You'll see why this is used. +* Brand - "Jeep" +* Make - "Liberty" +* Year - 2006 +* Owner - "Daniel S" +* Number of Wheels - 4 (you'll see why this is used) We COULD represent all those values as different variables, but that would get very hard to follow once you introduce more and more cars...An easier way to handle all that data is to use a class! In Python and other @@ -27,7 +27,7 @@ view - before you can create Objects using it. The generic way to declare a clas new term 'kwargs' which will be explained after.* ```python -class ClassName(object): # Tell Python you want a class and give it a name +class ClassName: # Tell Python you want a class and give it a name constant_1 = "value_1" # Declare any attributes you want all objects of that type to have. constant_2 = "value_2" @@ -44,14 +44,14 @@ to find things, you use actual strings or objects to map from one thing to anoth ```python stuff = {'name': 'Zed', 'age': 39, 'height': 6 * 12 + 2} -print stuff['name'] +print(stuff['name']) > Zed -print stuff['age'] +print(stuff['age']) > 39 -print stuff['height'] +print(stuff['height']) > 74 stuff['city'] = "San Francisco" -print stuff['city'] +print(stuff['city']) > San Francisco ``` @@ -61,9 +61,9 @@ put new things into the dictionary with strings. It doesn't have to be strings t ```python stuff[1] = "Wow" stuff[2] = "Neato" -print stuff[1] +print(stuff[1]) > Wow -print stuff[2] +print(stuff[2]) > Neato print(stuff) > {'city': 'San Francisco', 2: 'Neato', 'name': 'Zed', 1: 'Wow', 'age': 39, 'height': 74} @@ -78,9 +78,9 @@ And then to describe our car, we can create this: ```python # Declare the class with a name -class Car(object): # Declare a 'class' named 'Car' that is an 'object' +class Car: # Declare a 'class' named 'Car' - num_of_wheels = "4" # The number of wheels the car has. All cars have 4 wheels, so this is + num_of_wheels = 4 # The number of wheels the car has. All cars have 4 wheels, so this is # outside of the '__init__()' function because this value never changes. def __init__(self, brand, make, year, owner, **kwargs): # This function is called when you create an object of @@ -101,9 +101,9 @@ still being able to associate any other variables as well. So how would you use ```python # Declare the class with a name -class Car(object): # Declare a 'class' named 'Car' that is an 'object' +class Car: # Declare a 'class' named 'Car' - num_of_wheels = "4" # The number of wheels the car has. All cars have 4 wheels, so this is + num_of_wheels = 4 # The number of wheels the car has. All cars have 4 wheels, so this is # outside of the '__init__()' function because this value never changes. def __init__(self, brand, make, year, owner, **kwargs): # This function is called when you create an object of @@ -124,10 +124,10 @@ def create_car(): """ This will call a bunch of inputs and return a new Car object """ - brand = input("What is the brand of your Car?") - make = input("What is the make of your Car?") - year = input("What is the year of your Car?") - owner = input("What is your name?") + brand = input("What is the brand of your Car? ") + make = input("What is the make of your Car? ") + year = input("What is the year of your Car? ") + owner = input("What is your name? ") return Car(brand, make, year, owner) # This will create a Car object based on what we have above my_first_car = create_car() # Create one car using the function above