Section 1: Python Basics
Welcome to Section 1! In this section, we will cover the basics of Python. Let's get started!
1.1 Variables
In math class, recently or a long time ago, your teacher probably taught you about variables. In coding, we use variables all the time. Some ways to define a variable are ...
variable_name_1 = 10
variable_name_2 = 2.5
variable_name_3 = True
Now we have three variables assigned to 10, 2.5, and True, respectively. You can also reassign variables to different values after initially assigning them something else. An example of this is ...
variable_name = 15.5
variable_name = 55.1
Now the new value of this variable is 55.1. There is no way to retrieve the previous value of this variable, 15.5.
You have to follow many rules when naming a variable. Here are the rules you have to follow. Variable names ...
cannot be more than one word.
should make sense to the user.
can have both upper and lower case letters.
cannot start with a number.
cannot have any special characters.
can either use camelCaseNaming or snake_case_naming.
Now that you have variables down, let's move on to the next part!
1.2 Data Types
Every variable has a data type. We are going to start by covering 3 data types, integers, floats, and booleans.
You probably already know what integers are. Integers in Python are just whole numbers. Integers have no special rules that you have to follow. Some examples of integers are 0, 5, and 1234.
You probably also know what floats are. You would probably call them decimals. A float is just an integer with a decimal point and other numbers following it. Similarly to integers, there is no special rules that you have to follow for floats. Some examples of floats are 0.9, 8.7, and 123.456.
Booleans are something you might know as True and False. In Python, there are only 2 booleans, True and False. These are used in conditional statements (we will cover this in Section 4). The first letter in both booleans need to be capitalized every time they are used.
Now that you have data types down, let's move on to the next part!
1.3 Comments
When somebody creates a code, it is meant for somebody else to use and to assist others with a task. Whenever you create a code, the user of the code should easily be able to understand your code. If you want to write something in your code that is not in Python and doesn't affect anything, you would do something like this ...
#This is a comment. This doesn't do anything.
If you add anything in your code that starts with a #, it will not affect your output at all.
Now that you have comments down, let's move on to the next part!
1.4 Math Operators
Just like in regular math, there are a bunch of math operators in Python. The first few are simple and are similar to how you would write them down, they are ...
addition = 1 + 1 #2
subtraction = 2 - 1 #1
multiplication = 2 * 2 #4
division = 4 / 2 #2
The code above sets 4 variables to the answer of the math equation. The number in the comment is the new value of each variable and the answer of the equation.
The next three math operators might be a little more confusing to you. They are the following ...
exponentiation = 7 ** 3 #343
floor_division = 7 // 3 #2
modulo = 7 % 3 #1
The first one is 7 ^ 3. However, Python uses ** instead of ^. The answer to that is 343. The next one is 7 / 3 without the remainder. This is the same thing as always rounding the decimal down to the nearest integer. The answer to that is 2. The last one is just the remainder of the division. This is the same thing as multiplying the decimal part of the quotient by the divisor. The answer to that is 1.
Lastly, Python always follows the order of operations. The order of operations is PEMDAS (Parentheses, Exponents, Multiplication and Division, Addition and Subtraction). You should keep this is mind while coding long expressions.
Now that you have math operators down, let's move on to the next part!
1.5 Print
Whenever somebody creates a code, they will probably want to show the user what the code has done. Python will not automatically show the user of the code the output. A coder has to write a line of code if they want something to be displayed in the output. Here are a few examples ...
example_1 = 3.14
example_2 = 125
print(example_1)
print(example_2)
What this code will do is show the user our two numbers, 3.14 and 125, on different lines. The syntax of the print function is relatively simple. You need to type "print(...)", and inside the parentheses, you can put a variable.
example_3 = (4 / 2) ** 3
print(example_3)
print(2 + 3)
What this code will do is show the user the output of the equation assigned to example_3 and the output of the equation inside the print, 8 and 5.
Now that you have print down, let's move on to the next part!
1.6 Floats
As we have covered earlier, floats are numbers that have a decimal part. Even though floats are normally accurate, sometimes Python messes up and gives us ridiculous answers for simple math equations. One example is ...
print(1.23 + 2.80)
One would expect this to return 4.03. However, the code will sometimes return 4.0299999. This is an inbuilt error in Python. There is a few simple ways to fix this. One way is ...
print((123+280)/100)
This will return 4.03 as there is no addition of decimals but addition of integers, which doesn't cause an error.
If you preform a mathematical operation and your answer is too long and needs to be rounded, you can just use the round function. This can also be used to fix miscalculations. An example of this is ...
example_4 = 1.23 + 2.80
print(round(example_4, 2))
1.23 + 2.80 returned an extremely weird number. However, if we use the round function, it will output 4.03, the desired answer. The syntax for round is a little bit more complicated than print. You need to have "round(...)". Between the parentheses, you need to put in the float or expression that you want to round. Then, you have to add a comma and put in how many decimal places you want it rounded to. If you put in an invalid number, it will give you an error.
Now that you have floats down, let's move on to the next section!
Sample Problems