Introduction to Python
This section will cover the introduction to Python.
Table of Contents
- Overview
- Python Basics
- Strings
- Variables
- Operators
Overview
Python is a general purpose, object-oriented scripting language. It is mainly used to build applications, automate tasks, and data analysis.
Python Basics
This part will cover some fundamental knowledge required when scripting in Python.
Strings
A string is used to represent textual data. A string is a sequence of characters enclosed in single quotes (' ') or double quotes (" "). The below are some examples of strings.
'My String'
"My String in double quotes."
"string"
To visualise the data, we can print it to the terminal or screen using the print() function.
print("Hello World!")
print('Hello World with single quotes.')
From the above example, the use of single or double quotes works the same.
Variables
Variables can be used to store data. Variables do not need to be declared with any particular type, and can change type after they have been set. Multiple variables can also be declared on the same line.
The below are some examples of variables.
x = 10 #The variable 'x' is an int (integer).
x = "hello" #The variable 'x' is now an str (string).
var1, myvar = 'My first variable', 'my second variable'
We can visualise this with the following lines of code.
x = 10
print(x)
x = "hello"
print(x)
var1, myvar = 'My first variable', "my second variable"
print(var1)
print(myvar)
Running the above code, we can see that the data within the variable can change without any type declaration.
Operators
Python can perform math operations. An example will be using the + operator to add two values.
print(2 + 2)
The below table will list Python arithmetic operators. Arithmetic operations are used with numeric values to perform common math operations.
| Operator | Name | Example | Description |
|---|---|---|---|
+ | Addition | 10 + 10 | Add two or more numbers. |
- | Substration | 10 - 5 | Substrate two or more numbers. |
* | Multiplication | 5 * 2 | Multiply two or more numbers. |
/ | Division | 10 / 2 | Divides two or more numbers. |
% | Modulus | x % y | Find the remainder after dividing the numbers. |
** | Exponent | a ** b | Multiply the base with its power. |
// | Floor Division | 11 // 10 | Round down to the nearest whole number. |
Python is also able to perform comparison using the below operators.
| Operator | Name | Example | Description |
|---|---|---|---|
== | Equals | x == y | Checks if the value is the same. Return "True" if it is the same. |
!= | Not Equal | x != y | Checks if the value is different. Return "True" if it is different. |
> | Greater Than | x > y | Check if the value is larger. Return "True" if it is larger. |
< | Lesser Than | x < y | Checks if the value is smaller. Return "True" if it is smaller. |
>= | Greater than or equal to | x >= y | Check if the value is greater or equal to the value. Return "True" if it is greater or equal to. |
<= | Less than or equal to | x <= y | Check if the value is lesser or equal to the value. Return "True" if it is lesser or equal to. |
The below code will help visualise this.
x = 10
y = 5
z = 3
print( x + y) #Results in 15
print(x - y) #Results in 5
print(x * y) #Results in 50
print(x / z) #Results in 3.3333333333333335
print(x % z) #Results in 1
print(x ** y) #Results in 100,000
print(x // z) #Results in 3
print(x == y) #Results in False
print(x != y) #Results in True
print(x > y) #Results in True
print(x < y) #Results in False
print(x >= y) #Results in True
print(x <= y) #Results in False