-
Notifications
You must be signed in to change notification settings - Fork 1
Python Basics
Teaches the basics of the programming language Python. Specifically, printing to the screen, expressions, data types, string concatenation, and storing values into variables.
We are going to want to display text on the screen. The way we do that is by the use of a Python function called print(). We can print strings such as
print("Hello World")
Output:
>> Hello World
When coding in Python, there are both logical operators and mathematical operators that we can utilize for expressions. First, let us take a look at logical operators, which enable the code to make decisions based on the conditions provided by logic statements; statements or expressions created using logic operators.
"="
-
Assigns a variable to a value
-
Example:
x = 1 # Assigns the value ‘1’ to the variable x
y = 2 # Assigns the value '2' to the variable y
print(x)
print(y)
Output:
>> 1
>> 2
"=="
-
Tests a value
-
Example:
x == y # Tests if the value in the variable x
# is equal to the value in the variable y
print(x == y) # Should return false because x = 1 and y = 2
Output:
>> false
"!="
-
“Not equal”
-
Example:
x != y # Tests if the value in x is NOT equal to the value in y
-
Tests if the left value is greater than the value on the right
-
Example:
x > y #Tests if the value in x is greater than the value in y
-
Tests if the left value is greater than OR equal to the value on the right
-
Example:
x>=y #Tests if the value in x is greater than OR equal to the value in y
"<"
-
Tests if the left value is less than the value on the right
-
Example
x<y #Tests if the value in x is less than the value in y
"<="
-
Tests if the left value is less than OR equal to the value on the right
-
Example:
x<=y #Tests if the value in x is less than OR equal to the value in y
Now for mathematical operators, which let the code perform mathematical expressions, such as addition, subtraction, multiplication, division, and much more.
"+"
- Performs addition
print(2+2)
>> 4
"-"
- Performs subtraction
print(4-2)
>> 2
"*"
- Performs multiplication
print(9*9)
>> 81
"**"
- Creates an exponent
print(2**3) #prints 2^3
>> 8
"/"
- Performs division
print(6/3)
>> 2
"//"
- Performs integer division (truncates the final value)
print(7/2) #Truncates the decimal that is created from this expression
>> 3
- 7 divided by 2 is 3.5, but integer division will truncate the decimal, whether it be 3.1 or 3.9, // will make it 3
"%"
- Performs modulus (finds the remainder)
print(7%3) #Prints the remainder of this expression
>> 1
Data types refer to the kind of information that is being stored into a variable.
String concatenation is when you combine two strings to make a single string.
We can store certain values into variables using the logical operator "="
TBA