-
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.
Colons can be used to align columns.
Operator | Name | Description |
---|---|---|
col 3 is | right-aligned | $1600 |
col 2 is | centered | $12 |
zebra stripes | are neat | $1 |
"="
-
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)
Output:
>> 4
"-"
- Performs subtraction
print(4-2)
Output:
>> 2
"*"
- Performs multiplication
print(9*9)
Output
>> 81
"**"
- Creates an exponent
print(2**3) # prints 2^3
Output:
>> 8
"/"
- Performs division
print(6/3)
Output:
>> 2
"//"
- Performs integer division (truncates the final value)
print(7/2) # Truncates the decimal that is created from this expression
Output:
>> 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`
Output:
>> 1
Data types refer to the kind of information that is being stored into a variable. There are three types of data types we will be focusing on for now: ints, floats, and Strings.
An int type variable stores an integer; a whole number.
x=5 #Here, x is a variable with the data type int
A float type variable stores a decimal.
x=4.2 #This time, x is a decimal, so it is of the data type float
A string type variable stores letters, or text.
x="Hello World" #When storing a string, we use quotes, and here, x stores the string "Hello World"
String concatenation is when you combine two strings to make a single string.
x="Hello"
y="World"
print(x+y)
Output >> HelloWorld
We can store certain values into variables using the logical operator "=" as shown in the previous examples.
x=3 #The "=" assigns the value 3 to the variable x,
#And now when we call x, it represents the value 3
y=2.5 #And here, "=" assigns the value 2.5 to the variable y
#Now when we call y, it represents the value 2.5
TBA