├── 0. Introduction ├── 1. Introduction.md ├── 2. Where to Write Code.md └── 3. Programming Languages.md ├── 1. Your First Code ├── 1. Hello World.md ├── 2. Arithmetic.md ├── 3. Types.md ├── solutions1.py └── solutions2.py ├── 2. Variables ├── 1. Variables.md └── solutions1.py └── README.md /0. Introduction/1. Introduction.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | So, you want to learn how to code? Well, I know reading is really boring. I want to keep things really simple, use a lot of pictures, and a lot of examples. 4 | I also like using very casual language, because humans don't usually talk in terms like "Honorificabilitudinitatibus" and other English "syntactic sugar". 5 | I mean really, who cares? Let's cut the chit-chat and get right to business. 6 | 7 | ## "I DON'T KNOW WHERE TO START"... Ok, How About here? 8 | ```python 9 | # Let's say something fun 10 | print('chicken nugget') 11 | # Wow, I bet that does something cool! 12 | ``` 13 | 14 | I bet that's absolutely terrifying to look at, right? Like, what the hell does any of that mean? This is where you don't start. You're new to this, it's 15 | far too soon to throw you into the ocean yet. Let's slow it down and talk about some simple stuff first; really make sure to ease you into the new world 16 | you just entered. It's kind of like at the end of Captain America: The First Avenger where Steve Rogers is in the middle of a crazy futuristic New York. 17 | I'm Nick Fury right now, you're Steve Rogers. Don't worry, pal. I'll get you up to date in no time. 18 | 19 | ## Where to Start: Understanding Coding 20 | What in God's name is coding? It's pretty simple, let's give you a great example here. Imagine you only speak one language. It really doesn't matter 21 | *which* language it is, and I would rather leave it abstract. Otherwise, I'd get poor ratings on Twitter for not being inclusive enough. So let's say you 22 | have to travel to a different country for some business, but they speak a completely different language than you. It's really hard to communicate with them, 23 | isn't it? You're doing all these hand gestures to try and describe what any of your words mean. Now ironically, that's very similar to how we tell our computer 24 | how to do things. You speak a language, you translate your language into these simple (and awkward) hand gestures that both you and the other person (the computer) 25 | can understand. 26 | 27 | As a human, you say 'hi' simply by opening your mouth and saying it. A computer says something like this: `print('hi')`. There are some details that I am definitely 28 | saving for later because they don't matter right now. If you're a really smart Computer Science nerd with years of experience, please stop fuming. We'll get to it. 29 | 30 | Of course, humans kind of have it easy. When you want to talk to another human, you can do it whenever the hell you want. You are on Earth, you have the privilege 31 | of walking outside, finding a person, and saying 'hi' to them. That's pretty cool! You know what you can't do, though? You can't just write code anywhere. Let's 32 | try it, just for fun. 33 | 34 | ![I tried](https://cdn.discordapp.com/attachments/656379081264332814/799774640088023050/image0.jpg) 35 | 36 | As you can see, not a single damn computer listened to me. That line of code is sitting there, doing absolutely nothing for society other than wasting a precious 37 | piece of paper. So what did we learn from this? 38 | 39 | ## Where to Write Code: A Beginners Approach 40 | There are some great places to write code where you'll never have to worry about the pesky details on how to set up a project, or virtual environment, or any 41 | mumbo jumbo that you can just learn later. You're here to learn to code, not manage a file system. I understand. If you want to learn more about the setup process, 42 | there will be a section on it in here at some point, I can assure you of that. Remind me to come back and edit this part with a link to that part of the repo once 43 | it's added. But for those of us who just want to get to coding, we're going to use an online "IDE" ('Integrated Development Environment' but again, details for later) 44 | called repl.it. This is a fantastic site to start coding right away with nothing holding you back. For ease of visibility in this repo, I'll start a new section for 45 | setting up the site. See you there! 46 | -------------------------------------------------------------------------------- /0. Introduction/2. Where to Write Code.md: -------------------------------------------------------------------------------- 1 | # Where to Write Code 2 | 3 | ## I Know You Skipped my Intro, and I'm Sad 4 | Welcome to the new page! I know I know, kind of ridiculous. But not everyone starts in the same spot. Some people will just skip the juicy intro that I worked 5 | really hard on so that they can make 2 minutes of progress. I know many people will just stick to the basic script of everything, but I want to feel really 6 | connected to my audience, you know? Speaking of which, have you had enough water today? Enough to eat? Take a break real quick, you deserve it. 7 | 8 | ## Repl.it 9 | Ok spearheaded Computer Science specialists, I know. There are other tools out there, they are 'so much better' in your eyes, thank you for your humble opinion. 10 | Repl.it is super easy, and super simple. Wanna start writing code? Follow these steps: 11 | 12 | 1. Go to https://repl.it/ 13 | 2. Create an account. Simple, easy, fantastic, you're doing great. 14 | 3. Congratulations, now you have a place to write code. 15 | 16 | Notice something, by the way. You didn't have to download anything! That's the power of cloud computing! Wanna learn about that? Regardless, someone else will 17 | say yes, and I will make a section on it. 18 | 19 | ## Ok, But How do I WRITE Code on Here? 20 | You know what, that's a good question. Here are the steps for that: 21 | 22 | 1. Click the "+ New Repl" button. It should be in the top left 23 | 2. You'll get a pop-up window that asks for a `Language` and a `Name'. 24 | 3. This entire repo is dedicated to the `Python` computer language, so select that one 25 | 4. Name the Repl whatever you want! 26 | 5. Click 'Create Repl' 27 | 28 | You'll be redirected to a page that looks a lot like this: 29 | ![My Beautiful Repl](https://cdn.discordapp.com/attachments/656379081264332814/799780718283980801/Screen_Shot_2021-01-15_at_3.22.48_PM.png) 30 | Congrats, you're ready to write code! There is one more section in here about `programming languages` which is optional, but VERY MUCH recommended for a better 31 | understanding of what the hell you're doing. Either check it out, or move on. Whichever one suits you best. 32 | -------------------------------------------------------------------------------- /0. Introduction/3. Programming Languages.md: -------------------------------------------------------------------------------- 1 | # Programming Languages (Simplified) 2 | So glad you could make it! You're part of the 10% of responsible individuals who decided to start reading this part. I'm so very proud of you. 3 | 4 | ## What is a Programming Language? 5 | You might think that a computer programming language is something that a computer understands. The horrifying part is that almost all programming languages are *not* 6 | immediately understood by the computer. Languages come in several types, and they all build on one another. At the very bottom is the computer itself. You 7 | need a computer in order to use a computer programming language. That's why the paper example in the introduction didn't work. On top of that is the "Machine Language" 8 | which you need not concern yourself with. On top of the Machine Language is the "Assembly Language" which, again, you don't really need to worry about right now. 9 | Machine and Assembly are considered **low level** languages. If you're really shooting for the stars and want to get involved with these languages, then you're likely 10 | doing something far more advanced than what this repo has in store for you. 11 | 12 | There are also **middle level** languages which have many features of a **high level** language, but usually require you to manually manage memory. I know you probably 13 | don't get why memory management matters, but it really matters and it's also a pain in the ass to deal with. 14 | 15 | Finally, we arrive to **higher level** languages. These are the ones you've likely heard of like Java, Swift, and Python. They are considered "high level" because they 16 | use an "interpreter" or "compiler" in order for the code to be understood by the computer. As you move up this stack of language types, you tend to get closer and 17 | closer to a more "human" way of communication. The code itself looks more like English and requires less manual management of many different functions. For example, 18 | in the *C* language, you can't store a single word. You have to store a collection of individual characters. Now in reality, that's exactly what a language like Python 19 | has you do, but the language abstracts that detail away and just store that collection of characters as something called a "string". To you, it looks like a single word. 20 | But in reality, it's a collection of characters. 21 | 22 | ## So Why do Lower Level Languages Even Exist Then? 23 | Well first of all, the computer cannot actually understand the high level code. That code has to be compiled into assembly and machine code before the computer knows what 24 | to do with it. Additionally, lower level code is extremely fast and efficient. As you abstract it away, a lot of things are going on in the background. Some of those 25 | automations cause quite the slowdown. Python is known for being very dynamic, which is a benefit of its high level nature. But that causes it to be pretty slow. Low level 26 | languages are highly optimized, while Python is far generic. There are some ways to optimize Python with the use of **libraries**, but that is for larger projects. We 27 | will be working with much simpler code in this repo. **Libraries** will mainly be utilized to access tools that the standard Python library does not have. We will go over 28 | libraries later, anyways. For now, it's coding time. 29 | -------------------------------------------------------------------------------- /1. Your First Code/1. Hello World.md: -------------------------------------------------------------------------------- 1 | # Hello, World! 2 | You're really gonna hate this phrase soon. If you take up coding and decide to learn more languages than just Python, you'll be seeing this "Hello, World!" 3 | thing pop up in every single introduction. It's crazy. 4 | 5 | ## Your First Code 6 | Alright, you've got your Repl sitting there, waiting. It's time to write some code. You see that window with the cursor, blinking? To the left of the console. 7 | Yeah, you got it. Type this in there: 8 | ###### Example 1.1 9 | ```python 10 | print("Hello, World!") 11 | ``` 12 | 13 | Then, click the "Run" button at the top. Check the console on the right. Woah! What the hell? Did you just make the computer talk? Congratulations, you are now 14 | a wizard. 15 | 16 | ## Hang on, What did I Just Do? 17 | What you did was execute a **statement**. Statements are actions or commands that have some sort of byproduct to them. What you wrote was called a *print statement*. 18 | It simply tells the computer to output to the console. 19 | 20 | #### What gets outputted? 21 | Anything that you put between those two parenthases. In this case, it's the phrase "Hello, World!". This string of text has to be contained within single, or 22 | double quotes (Python allows both). Try to run *this* code: 23 | ###### Example 1.2 24 | ```python 25 | print(Hello, World!) 26 | ``` 27 | You got some funky red text, didn't you? It said something about a **syntax error** I bet. A *syntax error*, simply put, is when you use the computer language wrong. 28 | For our example, we used improper syntax for the print statement. The statement expected us to use quotes within the parenthases, but we did not. Python does not 29 | recognize *Hello, World!* because when reading a string of text, it expects quotes before and after, hence `"Hello, World!"` and not `Hello, World!`. 30 | 31 | Let's try something else. Maybe I want to print the words "Hello" and "World" independently: 32 | ###### Example 1.3 33 | ```python 34 | print("Hello,") 35 | print("World!") 36 | ``` 37 | Try running this code and see what happens. Weird, they printed on two separate lines. Why is that? In Python, each print statement you write creates a new line. You 38 | can get rid of this for any print statement by adding `, end=""` to the end of your print statement, inside the parentheses: 39 | ###### Example 1.4 40 | ```python 41 | print("Hello,", end="") 42 | print("World!") 43 | ``` 44 | There are a couple things to notice here. First, look at the first line. Notice how there is a comma after the end of the quotes. Print statements can take in several 45 | values, all separated by commas! So you can feed a print statement several different strings of characters, as long as you separate them by commas. The `end=""` is 46 | specifying that the end of the print statement should not do anything, rather than start a new line. Let's modify that code. What if we wanted to add the phrase, "The 47 | sun is shining!" to the end? 48 | ###### Example 1.5 49 | ```python 50 | print("Hello,", end="") 51 | print("World!") 52 | print("The sun is shining!") 53 | ``` 54 | What went wrong here? "The sun is shining!" is on a new line, but why? Didn't we tell the print statement *not* to do that anymore by adding `end=""` to the first 55 | print statement? Nope. We only told the *first* print statement to do that. Each print statement is independent of other print statements. So you have to specify 56 | `end=""` for every print statement that you want to apply that rule to: 57 | ###### Example 1.6 58 | ```python 59 | print("Hello,", end="") 60 | print("World!", end="") 61 | print("The sun is shining!", end="") 62 | ``` 63 | Let's look at one last thing. Remember how I mentioned earlier that you can add multiple strings of text to one print statement? Well... Why not just do that?!? 64 | ###### Example 1.7 65 | ```python 66 | print("Hello," , "World!" , "The sun is shining!") 67 | ``` 68 | Wait a minute, where did those spaces come from??? There are no spaces in "Hello," or "World!" so why did a space get added between them? When you use a comma in 69 | a print statement, it adds a space! For strings of text, you can avoid these unwanted spaces by using **concatenation** instead. That's a fancy term for "replace the 70 | comma with a plus": 71 | ###### Example 1.8 72 | ```python 73 | print("Hello," + "World!" + "The sun is shining!") 74 | ``` 75 | Now, all those spaces that we never typed are no longer there! For this specific text, it's pretty silly to do since we need those spaces, but here's some code that 76 | would make it useful: 77 | ###### Example 1.9 78 | ```python 79 | print("Hel" + "lo," , "Wor" + "ld!" , "The" , "sun" , "is" , "shining!") 80 | ``` 81 | Notice how I used the `+` to concatenate the split words, and the commas as delimiters for whole words. So the `+` can be used to combine pieces of text, and the `,` 82 | can be used to space-separate text. 83 | 84 | ## Further Explorations 85 | ###### Here are some mini challenges for you to try out. You can find examples 1.1 through 1.9 and solutions to the Further Exploration problems in solutions1.py 86 | 87 | 1. Change the print statement so that it instead outputs `Hello, Universe!` instead 88 | 2. Print your first name, last name, and age on three separate lines 89 | 3. Now print your first name, last name, and age on the same line, using three different print statements 90 | 4. Finally, print your first name, last name, and age using one print statement, but three different strings of text 91 | 5. Print the following strings of text: `"Man," , "lo" , "I" , "Chick" , "ve" , "-fil" , "-a!"` such that the output is `Man, I love Chick-fil-a!` 92 | -------------------------------------------------------------------------------- /1. Your First Code/2. Arithmetic.md: -------------------------------------------------------------------------------- 1 | # Arithmetic 2 | ###### You're brave for getting past that print mumbo jumbo. Time for some numbers! 3 | 4 | ## We Can Do Math! 5 | That's right, it's time for some numbers work. You can do a bunch of math in Python, and easily see it working. For starters, let's add a new line of code to your 6 | repl: 7 | ###### Example 2.1 8 | ```python 9 | print(2 + 2) 10 | ``` 11 | Run that. What did the console output? I bet it was 4. Was it 4? Dang, I am so good at this. That's right, the print statement accepts numbers and math, too! You can 12 | combine numbers AND text: 13 | ###### Example 2.2 14 | ```python 15 | print("The United States has" , 25 + 25 , "states!") 16 | ``` 17 | But Python can do more than just addition. Let's try a few different operations, shall we? How about we start with the four main **operators**: 18 | ###### Example 2.3 19 | ```python 20 | print(2 + 2) 21 | print(10 / 2) 22 | print(5 * 5) 23 | print(10 - 5) 24 | ``` 25 | If you run this code, you may notice that the results are printed, but it's just a bunch of numbers, which are hard to interpret as a reader. Let's add some text 26 | to the print statement using the skills you learned in the last section in order to make the output more readable: 27 | ###### Example 2.4 28 | ```python 29 | print("2 + 2 =", 2 + 2) 30 | print("10 / 2 =", 10 / 2) 31 | print("5 * 5 =", 5 * 5) 32 | print("10 - 5 =", 10 - 5) 33 | ``` 34 | That's much better. Now you can see some of the **operators** you can use for simple math. `+` can be used for addition, `-` for subtraction, `*` for multiplication, 35 | and `/` for division. But wait a second, why did the stuff in quotes not evaluate to any result? Aren't those numbers, too? Yes, they are numbers! But they are wrapped 36 | in quotes, so they are read by Python as a "string" type. Let's go on to the next section to discuss those. But first, play around with the arithmetic a bit. 37 | 38 | ## Further Explorations 39 | ###### Here's a few things to try on your own to better understand arithmetic in Python. You can find all of the above examples, as well as solutions to the Further Explorations problems in solutions2.py 40 | ``` 41 | Use the following information for your exploration problems: 42 | Laura is 6, Jane is 12, Max is 15, George is 22 43 | ``` 44 | Utilizing the operators you learned in this section and the information above, perform the following tasks: 45 | 1. Write a print statement that outputs `Laura is 6 and Jane is 12, the sum of their ages is 18` 46 | 2. Write a print statement that outputs `The difference in age between Laura and Max is 9 years` 47 | 3. Write a print statement that outputs `George is 3.6666666666666665 times older than Laura` 48 | 4. Write a print statement that outputs `If I drive 60 miles per hour, and I drive for 4 hours, I will drive 240 miles` 49 | -------------------------------------------------------------------------------- /1. Your First Code/3. Types.md: -------------------------------------------------------------------------------- 1 | # Types 2 | ###### I think the fact that you're still reading these is very admirable. You're really dedicated. Almost through part 1. That's crazy. 3 | 4 | ## What The Hell is a Type? 5 | What is this: `6`? You probably said that it's either a six (ok smartass) or a *number*. In coding lingo, these are called *Integers*, and in Python, their type 6 | is `int`. Ok ok, what about `6.1`? Don't say "six point one". It's a number, but not quite the same as the other number. Numbers with decimal points like this 7 | are referred to in coding lingo as either *doubles* or *floats*. I assume that "double" refers to the fact that there are really two numbers separated by a 8 | period, otherwise the name really comes out of left field for me. In Python, their *type* is `float`. How about this: `"Chicken"`? That doesn't look like a number 9 | to me! You certainly can't do `"Chicken" / "Chicken"` like you can `6 / 6`. This must be something different! And it is! It's a string of characters, or what the 10 | computer geeks call a *string*. In Python, this is the `str` type. Finally, we have `True` and `False`. Oh easy, these are strings, right? Well, no. They lack the 11 | quotes that strings have. So what the hell are they? These are very nerdily called *booleans*. A boolean is one of two things: true, or false. Simple as that! In 12 | Python, their type is `bool` because Python is straight boolin. I hope most of you aren't too old for that term. 13 | 14 | You can easily print the type of any value by using the `type()` method in Python. It works like this: 15 | ```python 16 | print(type(True)) # Prints 'bool' 17 | print(type(6)) # Prints 'int' 18 | print(type(6.5)) # Prints 'float' 19 | print(type("Hello")) # Prints 'str' 20 | ``` 21 | ###### Notice the `#`? Those are used to signify *comments*. These are used to leave little notes in your code. Comments span from the `#` to the end of the line 22 | 23 | ##### Output: 24 | ``` 25 | bool 26 | int 27 | float 28 | str 29 | ``` 30 | As you can see, these are four common types in Python. There are other more complex types that we will work with later, but these are some of the basic ones that we 31 | can use as building blocks, so we can work our way up to more complex code. 32 | -------------------------------------------------------------------------------- /1. Your First Code/solutions1.py: -------------------------------------------------------------------------------- 1 | """Welcome to the code-along! In each section, I will 2 | include a Python file with everything we do in the 3 | section so you have easy access to it all. 4 | """ 5 | SECTION = "\n***********************************\n" 6 | 7 | 8 | # 1. Hello, World! 9 | print("1. Hello, World!", SECTION + "Example 1.1:") 10 | # Example 1: 11 | print("Hello, World!") 12 | 13 | 14 | # Example 2 does not work! 15 | # print(Hello, World!) 16 | 17 | 18 | print(SECTION + "Example 1.3:") 19 | # Example 3: 20 | print("Hello,") 21 | print("World!") 22 | 23 | 24 | print(SECTION + "Example 1.4:") 25 | # Example 4: 26 | print("Hello,", end="") 27 | print("World!") 28 | 29 | 30 | print(SECTION + "Example 1.5:") 31 | # Example 5: 32 | print("Hello,", end="") 33 | print("World!") 34 | print("The sun is shining!") 35 | 36 | 37 | print(SECTION + "Example 1.6:") 38 | # Example 6: 39 | print("Hello,", end="") 40 | print("World!", end="") 41 | print("The sun is shining!", end="") 42 | 43 | 44 | print(SECTION + "Example 1.7:") 45 | # Example 7: 46 | print("Hello," , "World!" , "The sun is shining!") 47 | 48 | print(SECTION + "Example 1.8:") 49 | # Example 8: 50 | print("Hello," + "World!" + "The sun is shining!") 51 | 52 | 53 | print(SECTION + "Example 1.9:") 54 | # Example 9: 55 | print("Hel" + "lo," , "Wor" + "ld!" , "The" , "sun" , "is" , "shining!") 56 | 57 | print(SECTION) 58 | print("Further Exploration Solutions") 59 | 60 | 61 | print(SECTION + "Problem 1:") 62 | # Problem 1: 63 | print("Hello, Universe!") 64 | 65 | 66 | print(SECTION + "Problem 2:") 67 | # Problem 2: 68 | print("John") 69 | print("Doe") 70 | print("50") 71 | 72 | 73 | print(SECTION + "Problem 3:") 74 | # Problem 3: 75 | print("John", end=" ") 76 | print("Doe", end=" ") 77 | print("50", end="") 78 | 79 | 80 | print(SECTION + "Problem 4:") 81 | # Problem 4: 82 | print("John", "Doe", "50") 83 | 84 | 85 | print(SECTION + "Problem 5:") 86 | # Problem 5: 87 | print("Man,", "I", "lo" + "ve", "Chick" + "-fil-" + "a!") 88 | -------------------------------------------------------------------------------- /1. Your First Code/solutions2.py: -------------------------------------------------------------------------------- 1 | """Section 2: Arithmetic 2 | """ 3 | SECTION = "\n***********************************\n" 4 | 5 | 6 | # 2. Arithmetic 7 | print("2. Arithmetic", SECTION + "Example 2.1:") 8 | # Example 1: 9 | print(2 + 2) 10 | 11 | 12 | print(SECTION + "Example 2.2:") 13 | # Example 2: 14 | print("The United States has" , 25 + 25 , "states!") 15 | 16 | 17 | print(SECTION + "Example 2.3:") 18 | # Example 3: 19 | print(2 + 2) 20 | print(10 / 2) 21 | print(5 * 5) 22 | print(10 - 5) 23 | 24 | 25 | print(SECTION + "Example 2.4:") 26 | # Example 4: 27 | print("2 + 2 =", 2 + 2) 28 | print("10 / 2 =", 10 / 2) 29 | print("5 * 5 =", 5 * 5) 30 | print("10 - 5 =", 10 - 5) 31 | 32 | 33 | print(SECTION) 34 | print("Further Exploration Solutions") 35 | 36 | 37 | print(SECTION + "Problem 1:") 38 | # Problem 1: 39 | print("Laura is", 6, "and Jane is", 12, ", the sum of their age is", 6 + 12) 40 | 41 | 42 | print(SECTION + "Problem 2:") 43 | # Problem 2: 44 | print("The difference in age between Laura and Max is", 15 - 6, "years") 45 | 46 | 47 | print(SECTION + "Problem 3:") 48 | # Problem 3: 49 | print("George is", 22 / 6, "times older than Laura") 50 | 51 | 52 | print(SECTION + "Problem 4:") 53 | # Problem 4: 54 | print("If I drive", 60, "miles per hour, and I drive for", 4, "hours, I will drive", 60 * 4, "miles") 55 | -------------------------------------------------------------------------------- /2. Variables/1. Variables.md: -------------------------------------------------------------------------------- 1 | # Variables 2 | Look at you, being all productive and making it to part two. My goodness. Congratulations! 3 | 4 | ## Oh God, This Sounds Like Math... 5 | Well, yes and no. The good news is that in computer programming, variables are a bit more verbose and easier to swallow. Look at this lovely snippet of code: 6 | ###### Example 2.1 7 | ```python 8 | name = "Chris Evans" 9 | age = 39 10 | hotness_scale = 9.9 11 | star_spangled = True 12 | has_plan = True 13 | ``` 14 | So here, we've made some `variables`. These are useful because they can be stored and reused under their alias later. the `name` variable holds a 15 | `str` value of `"Chris Evans"`, the `age` variable holds an `int` value of `39`, the `hotness_scale` (out of 10) holds a `float` value of `9.9`, the 16 | `star_spangled` variable holds a `boolean` value of `True`, and the `has_plan` variable holds a `boolean` value of `True` as well. That's right, Chris Evans 17 | may be 39 years old but he's still a 9.9/10 on the hotness scale. And he's the star-spangled man with a plan. Don't you forget it. 18 | 19 | ## How Do We Use These? 20 | It's pretty simple. Create a new Repl.it project using Python, name it `CaptainAmericanPy` because this is Captain America we are working with here. Once it's 21 | created, copy this code into it: 22 | ###### Example 2.2 23 | ```python 24 | name = "Chris Evans" 25 | age = 39 26 | hotness_scale = 9.9 27 | 28 | print(name, "is", age, "years old, but he is still a", hotness_scale, "out of 10 on the hotness scale.") 29 | ``` 30 | As you can see here, you just drop the variable names in where you want to use them, and then BOOM, they print as the values they hold. It's really nice that 31 | this didn't print `name is age years old, but he is still a hotness_scale out of 10 ont the hotness scale.` becuase that would be so dumb and worthless. Like what 32 | would the point in variables even be? 33 | 34 | ## I Do NOT Want to Type That Many Commas 35 | Ok fine, I'll show you a little trick. It's called *formatting*. Watch this: 36 | ###### Example 2.3 37 | ```python 38 | name = "Chris Evans" 39 | age = 39 40 | hotness_scale = (2 * 4) + 1.9 41 | 42 | print('{0} is {1} years old, but he is still a {2} out of 10 on the hotness scale.'.format(name, age, hotness_scale)) 43 | ``` 44 | WOAH, what??? Calm down, I know that those curly braces look very intimidating but they are simple when you look at everything that is going on. Let's take a 45 | look at the very end of the line. So we give the print statement the following string: `'{0} is {1} years old, but he is still a {2} out of 10 on the hotness scale.'` 46 | which is fine and dandy, just like a normal print statement. If that's all we did there, then it would print with all of the curly braces in it, which would be 47 | super duper ugly. But notice that the print statement doesn't end there. there is a call to `format` and we passed in our three variables. The order in which you 48 | place these variables into the `format` function is very important. Look at how `name` is first. Now look back at the string and see that `{0}` is replaced with the 49 | `name` variable. That is because `name` is the first variable passed. 50 | 51 | ## But... Why Zero? 52 | Python, and most other programming languages, do something called **zero-based numbering**. That's a fancy way of saying that the first element of any list or structure 53 | is at *index 0*. Here is a visual aid to help you comprehend what that means: 54 | ###### Your normal, human brain type list: 55 | 1. Eggs 56 | 2. Cheese 57 | 3. Milk 58 | 59 | ###### The computer, for some reason does this instead: 60 | 0. Eggs 61 | 1. Cheese 62 | 2. Milk 63 | 64 | We will dive deeper into this and the implications of it later. Right now, you just wanna build a solid understanding of variables. 65 | 66 | ## Important Terms 67 | When we talk about variables, we need to sound kinda smart. Or at least communicate well so people know what we are referring to. There are a few important distinctions 68 | you should make when talking about variables, and not all of them actually apply to Python. First off with variables, you can `Declare`, `Initialize`, and `Assign`. 69 | Declaration isn't really a concept in Python, but it is in other languages. Declaration is when you create a variable, but it has no value. In another language called 70 | Java, you can delcare a variable like this: 71 | ###### Example 2.4 72 | ```java 73 | String name; 74 | ``` 75 | As you can see, we never gave the `name` variable any value here. We just said that there is a `String` variable with the alias `name` and we will define its value 76 | later. Initialization is more Pythonic. Every variable in Python is initialized. What that means is that we give it an initial value: 77 | ###### Example 2.5 78 | ```python 79 | name = "Chris Evans" 80 | ``` 81 | Then there is variable assignment. An assignment is when you modify a variable to be something else than what it was initialized to be: 82 | ###### Example 2.6 83 | ```python 84 | name = "Chris Evans" 85 | new_name = "Steve Rogers" 86 | name = new_name 87 | ``` 88 | We initialized `name` and `new_name` and then assigned the *value* of `new_name` to be the new value of `name`... because Chris Evans is now Steve Rogers forever. 89 | 90 | There are also things called **constants** which are the *opposite* of variables. Their value cannot be changed. Again, in other languages you can specify that 91 | a variable is a `const` and the code will not run if you try to change a `const`. In Python, we can mimic that behavior in a more complex manner that we can get 92 | into when we start writing more complex code. For now, the best thing to practice is to name values you *don't* want changed in all caps, with underscores replacing 93 | spaces: `SOCIAL_SECURITY = "51...sike"` 94 | 95 | ## Speaking of Names... 96 | Let's talk about a couple of things. Python uses *snake case*. What the hell does that mean? It's a way to keep consistency in your code. When you name variables, 97 | make all the words lowercase, and separate the words with underscores. Like `kitchen_timer`, `actor_name`, and `is_clear`. Variable names cannot start with numbers 98 | or symbols, always a letter or word. Variable names can include numbers in them as long as they are not at the start. Also, naming your variables is very important. 99 | Please be fairly verbose with your variable name choice; make your names explain themselves. A good name for a variable which would store the flavor of ice cream that 100 | a customer wants would be something like `ice_cream_flavor`, not `x` or `icf` or `ice_crm_flvr`. Do not abbreviate, but get to the point. Variable names should not be too 101 | long. This stuff is an art, people. 102 | 103 | ## All The Right Types 104 | You may notice, as you start to explore the world of programming a bit more, that other languages will require you to specify the `type` of a variable like such: 105 | ###### Example 2.7 106 | ```java 107 | String name = "Steve Rogers"; 108 | ``` 109 | That is a snippet of a variable initialization in Java. Notice the `String` at the beginning. That's because Java does not infer the type of a variable at the time 110 | of initialization. Python has type inference, which means you don't specify the type. Python is smart enough to infer the type of your variables. That's also why 111 | you can declare variables in Java, but not Python. So it's nice, but not the best. 112 | 113 | ## Further Explorations 114 | ###### Complete the following problems to show you actually learned anything 115 | Use the following info for the exploration problems: 116 | `My name is Cameron. I am 21 years old. I love chocolate. My favorite number is the result of 2 * 11.` 117 | 0. Store my name, age, whether or not I love chocolate (hint: this is a yes or no question...), and my favorite number (do the calculation in the variable assignment!) 118 | as *variables* named: `name`, `age`, `chocolate_lover`, and `favorite_number` 119 | 1. Print `Welcome to the Avengers, Cameron` 120 | 2. Now print `Cameron, you are only 21. To be an Avenger, you have to pass a test` using the `format()` function to pass in variables 121 | 3. Now, print `Cameron, at 21 you have passed the test because your favorite number is 22, and because you have a True love for chocolate.` using the `format()` function 122 | to pass in variables. You will use EVERY variable for this last one. Look closely. 123 | 4. Name a constant variable that will store the side length of a Rubik's Cube, and then initialize it with a value of 6. 124 | 5. Does Python allow for variable declarations? If not, explain why, and what it would need in order to allow them. 125 | -------------------------------------------------------------------------------- /2. Variables/solutions1.py: -------------------------------------------------------------------------------- 1 | # Example 2.2 2 | name = "Chris Evans" 3 | age = 39 4 | hotness_scale = (2 * 4) + 1.9 5 | 6 | print(name, "is", age, "years old, but he is still a", hotness_scale, "out of 10 on the hotness scale.") 7 | 8 | #Example 2.3 9 | print('{0} is {1} years old, but he is still a {2} out of 10 on the hotness scale.'.format(name, age, hotness_scale)) 10 | 11 | # Example 2.6 12 | name = "Chris Evans" 13 | new_name = "Steve Rogers" 14 | name = new_name 15 | 16 | 17 | # Further Explorations 18 | name = "Cameron" 19 | age = 21 20 | chocolate_lover = True 21 | favorite_number = 2 * 11 22 | 23 | # Problem 1 24 | print("Welcome to the Avengers,", name) 25 | 26 | # Problem 2 27 | print('{0}, you are only {1}. To be an Avenger, you have to pass a test'.format(name, age)) 28 | 29 | # Problem 3 30 | print('{0}, at {1} you have passed the test because your favorite number is {2}, and because \ 31 | you have a {3} love for chocolate.'.format(name, age, favorite_number, chocolate_lover)) 32 | 33 | # Problem 4 34 | rubiks_side_length = 6 35 | cube_side_length = 6 36 | side_length = 6 37 | 38 | # Problem 5 39 | """ 40 | Python does not allow for variable declarations because the language does type 41 | inference. In order to allow for variable declaration in Python, the language 42 | would need to add a way to specify the type of a variable upon creation, or have 43 | a command to declare a variable. 44 | """ 45 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Cameron's Python Examples 2 | 3 | ## Welcome to Python Code Examples! 4 | 5 | This GitHub repo is here to teach you some of the simple concepts about coding in one of the simpler programming languages: Python! Throughout this GitHub repository, you will find some great resources on how to code, including various skills that may *not* JUST be coding. 6 | 7 | That's right, coding develops other skills. In fact, a lot of being a programmer is not about learning a coding language. It's about being a *problem solver*. And a lot of the time, it's not *other people's* problems you will be solving, but your own. Code almost never works as intended when you first run it. It's important that you learn some essential skills along the way that will help you in solving such issues--they happen far too often to be ignored. 8 | 9 | To start off, here are some great reasons to start coding, from various sources: 10 | 11 | #### The Muse: 12 | ###### (Source: https://www.themuse.com/advice/4-major-reasons-you-need-coding-skills-even-if-you-dont-want-to-be-an-engineer) 13 | 1. It will make you more self-sufficient 14 | 2. It will teach you how to think 15 | 3. It will improve your communication and collaboration skills 16 | 4. It can take your career to new heights 17 | 18 | #### Medium: 19 | ###### (Source: https://medium.com/better-programming/here-are-5-great-reasons-why-you-should-learn-to-code-9e496cf82026) 20 | 1. You can turn an idea into an app 21 | 2. Programs keep your mind busy and active for life 22 | 3. Coding teaches you how to approach problem solving 23 | 4. Automate daily tasks and save time 24 | 5. Job security and a great salary 25 | 6. It's a fun hobby! 26 | 7. A vibrant and friendly community *(I'd debate this one. StackOverflow can be toxic and gatekeepers are all over the place, don't let it get to you!)* 27 | 28 | #### My reasoning (This is in no particular order): 29 | ###### (Source: Me.) 30 | 1. The world has gone digital, it's time you start speaking the langauge 31 | 2. Understanding the *non-scifi* capabilities of code will help you better understand your own security and privacy online 32 | 3. Improve your logic skills like crazy 33 | 4. Someone captured electricity in a rock and with code, you can control that electricity. How cool is that? 34 | 5. You can brag to your friends that you know how to code *(Although please don't)* 35 | 6. You can learn to use the Terminal/cmd, and look like a hacker, without even hacking! 36 | 7. Learning to code is extremely rewarding! You'll feel so much happiness when you get each program to work. 37 | 8. I'm making it nice and easy--baby steps! 38 | 9. Your grandparents would be proud 39 | 10. If you read all these points, it's very obvious that you're invested enough to at least start. What are you waiting for??? 40 | 41 | 42 | As you can see, there is plenty to look forward to, so start exploring! This repo is a work-in-progress. If it doesn't look complete or comprehensive yet, that's because it isn't yet. Keep an eye out for updates! 43 | 44 | --------------------------------------------------------------------------------