Welcome back, everyone. Or for those who weren't here last week, welcome! You can review the Class 1 post I made to catch up on what you missed. Glad to "see" all of you here this week :)
If a lot of what was covered this week was new information, and it was hard to keep up, don't worry. A lot of fundamental topics were covered this week and I imagine if you haven't heard of them before it must be overwhelming. Take your time reviewing these lesson notes - I'm gonna try to write them in detail, and as always, reach Alan or me through email or Discord if you need more help. I'm available most of the time, but I am in a different time zone (EST) so I'm 3 hours ahead of you guys in PST. Just keep this in mind - but I will always answer within 24h (at the very most).
-Bonnie
10-10:10 -> Review from last class; check previous post for details 10:10-10:16 -> New arithmetic syntax
10:16-10:23 -> Review questions 10:23-10:40 -> Variables 10:50-11:04 -> Variable Activity
11:04-11:? -> User input and Typecasting 11:?-11:30 -> Some of the homework HOMEWORK (Questions 6-8) - https://bit.ly/3AnYjCE (more info)
9/25 - HOMEWORK SOLUTIONS ARE UP ABOUT IDES - Feel free to use whatever IDE you like. Visual Studio Code, repl.it, anything is fine. Ideone.com is just Alan's suggestion. If anyone would like specific help with homework in the future, we will probably use repl.it (it's like Google Docs for code) and it's easy to use - feel free to familiarize yourself with it in advance anyways if you feel like it. Repl.it would probably be my recommendation for an IDE if some of you guys are still deciding. FOR THOSE OF YOU WHO MISSED THE SECOND CLASS (I think maaybe there were one or two) - there is a separate link for the second class. It's been emailed out and it's in the Discord as well. Make sure to join the second link for the second class. There are two links. Just in case this is still an issue. I'll try to keep tabs on who is still missing the second class next week and reach out to them if the problem persists.
ARITHMETIC SYNTAX
% - Modulus
** (x/x) - Roots
This is some new Python arithmetic syntax that we didn't cover last week. Modulus is a new operation, and it is division, but it returns the remainder instead of the quotient. So 9 mod 3 (or 9 % 3) would equal 0, instead of the regular 3. 16 % 5 == 1, 3 % 1 == 2, and so on. The roots one is pretty simple, maybe you guys have covered this in math class, but raising a number to the power of a fraction is the same as rooting it by the inverse (the inverse is switching the numerator and denominator). So 16 ** (1/2) is 16 root (2/1), which both equal 4. Another example: 27 ** (1/3) == 27 root (3/1) == 3.
REVIEW QUESTIONS
Answers: 0, -21.5 Solutions: 1 + 14//(3*4) - 2 2 * 3/4 + 1 - 24 = 1 + 14//12 - 2 = 1.5 + 1 - 24 = 1 + 1 - 2 = -21.5 = 0 VARIABLES
General information: Variables are very, very important for programming. They make it so you can reference any piece of information as many times as you want without having to type it out again. All you have to do is give the variable's name and the computer will know its value. Because of this, each variable's name has to be unique. Variable names are case-sensitive, so C = 0 and c = 0 are different variables. Think of variables like the bar codes at stores. Each one is unique to its product and when the cashier scans the bar code, the register will know what item it is. Any bar code can be for any product, and any declared variable can hold any data. (This last point is just for Python - in other languages, data type matters.)
Declaring variables: In other languages, this part has more steps, but in Python, it's been made really easy. Variables are usually declared at the top of your program, so at the beginning of your code, write out a line with a variable name and a value separated by just one equal sign (e.g. a = 21, b = "hello", c = -10). The spaces are optional but they make it look nicer!
Initialization: Initialization is the process of setting a value to a declared variable. In Python, this happens in the same line as declaration (which is giving a variable a name, "declaring" the name to the computer). my_name = "Bonnie" < this is the initialization ^ this is the declaration
Naming conventions:
An example of a naming convention is demonstrated above with the "my_name" variable. Naming conventions are a set of rules you follow for naming certain parts of your program. They make it easier to know what parts of the program the name refers to just based on the way it is written.
We have learned about variables thus far, and the naming convention for these is called snake case. With snake case, every word in the name starts with a lower case, and the spaces are underscores (telephone_nums, time_of_day, num_students (num is short for number)). So when you use snake case to write your variable names, everyone who reads your program will know that those names are for variables. it_is_very_distinctive. There are different naming conventions that you'll learn as there are many things besides variables to name in programming.
Data Types:
Data types are the types that information can be in programming. These types are mostly universal. When we read information, we don't need a specific distinction as to what type it is - we just read it and understand. But for computers, and especially in different languages (Python is relatively streamlined), it is important to recognize the types of data they receive as only certain things can be done with certain types. You can't do math with words, right? So you have to make sure things like data types are regulated in your code.
Integers - all whole numbers, negative or positive. (-1000, 420.)
Strings ("") - denoted by the double "" quotations. Anything can be a string if it has these quotes around it. ("hello", "905-192-1119".) I mentioned above that you can't do math with words, but there is a special operation that you can only do with strings called concatenation. This is combining strings together. Just use the addition sign and the computer will return a string that is all of the strings you gave it added together. You could also use single quotes if you felt like it (I do since it's less keys to press and I am lazy haha) but just know that strings are supposed to use double quotes.
a = "hello" print("my name" + " is Bonnie.") b = "there" ---------------------------------------- message = a + " " + b + "!" "my name is Bonnie." print(message)
---------------------------- "hello there!" Floats - "Doubles" were also mentioned in class but these don't exist in Python. In other languages, there are size limits to the numbers you can store in specific data types, and the double is just a larger float, but don't worry about it. Floats are all numbers of any size with decimals (2.0, 3.14~, 9000.0000000000001). Characters ('') - denoted by the single '' quotations, this one is just like the double in the way that it is not distinct a data type in Python. Characters are just one letter ('c', 'C', 'z'). But you could write "c" or "C" or "z" and the program would be fine since the character type does not exist. You are just learning this for a fuller understanding of the various data types, and it would be useful if you chose to learn other programming languages.
Booleans - Booleans can store two values, True or False. In Python, these values are capitalized, so write them as such when assigning values in your code. In binary, which is the language that the computer ultimately speaks, 0 is False, and 1 is True. (enrolled = True, reading_lesson_notes = True) None - This is exactly what it sounds like. A None-type variable has nothing inside. :(
(a = None.)
VARIABLE ACTIVITY
Create one variable of each data type and print each of them out.
Alan's work:
Output: --------------------------- 1, 2.0, "Hello", 'c', False USER INPUT input("message") - This is a built-in function that Python has that allows the programmer to accept user input. So anyone who runs this program can type in some information for you to use in your code. Whatever you write in the brackets is what the computer prints to the console before just before accepting user input. The information the input() function receives will always be String type. fav_colour = input("What's your favourite colour? Enter it here: ") print("My favourite colour is " + fav_colour + " too!") -------------------------------------------------------------------- What's your favourite colour? Enter it here: yellow -> My favourite colour is yellow too!
-> the program pauses here while it waits for a response. once I type in "yellow" and hit enter, it will unpause and continue on to execute the print statement.
Typecasting - As data types are important in programming, and as the information the input() function receives is always a string, now is the time to learn about typecasting. This is how you change the type of data. int(), float(), and str() are the built-in functions we use to do this. Anything in the brackets of those functions is changed into that type.
user_num = int(input("Give me a number: "))
print(user_num // 3) ->
--------------------------------------------------------- Give me a number: 9 3
-> it is important that we convert to an integer so that we can do math with the user input without raising an error.
HOMEWORK Yes, very sad, we have homework this week. Here is the link again: https://docs.google.com/document/d/1grO8moaBkKKICW-MSgEwKuIrxc9OthtZsKZtJ2FZCTc/edit. You only need to do Questions 6-8, so it's not that bad. I'll post full solutions by Saturday night, but ask Alan or me for help throughout the week if you need it. Actually try to do the homework, guys - if it's hard, it means it's good practice, and you can still ask for help anyways, and if it's easy, then you'll be done in no time. Good luck :D
HOMEWORK SOLUTIONS ARE UP - 9/25 Thanks for your time this week, everyone. See you soon. -Bonnie (and Alan)