Python 101
python |
Python is an interpreted high level programming language. And its a beginner language because is more of English e.g print("I love coffee")
the above statement is a python snippet, let me break it down
print - Its a function in python for displaying on the terminal.
( ) - Used to enclose the contents to be printed. in python 3.x is mandatory unlike python 2.x (discontinued) which wasn't.
" " - Encloses a string to be displayed on the terminal.
If you are using windows operating system you can have python installed by browsing to https://www.python.org/ to download the latest version of python which is python 3.8.2 and if you are a Linux user python is already pre-installed.
Basics
By typing in the terminal python, you will get a prompt like this:-
hello world |
script hello.py |
Variables
Variables are data containers, which are declared by assigning values to them.
x = 10name = "aimsec"
price = 99.9
try it on your python prompt.
variables |
in order for us to see the data held in the variables we use the print function.
printing variables |
Data Types
Data type is simply a classification of the value or data a variable contains, we have various data types:-
- Strings - str (This are a chain of characters) e.g x = str(" aimsec ")
- Integer - int (This are 4 byte numerical values) e.g x = int(43)
- Float - float (This are 4 byte decimal values) e.g x = float(9.99)
- Boolean - bool (This contains either True or False)
e.g isLate = True
print(bool(isLate))
Operators are used to perform operations on variables and values. Operators are classified into various categories:-
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
Arithmetic Operators.
Are used to perform mathematical functions
Operator | Name | Example |
+ | addition | x + y |
- | subtraction | x - y |
* | multiplication | x * y |
/ | division | x /y |
% | modulus | x % y |
** | Exponentiation | x ** y |
Assignment Operators.
Are used to assign values to variables.
Operator | Example | Equivalent |
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
- = | x - = 5 | x = x - 5 |
* = | x * = 5 | x = x * 5 |
/ = | x / = 5 | x = x / 5 |
Comparison Operators.
This operators are used to compare two values.
Operator | Name | Example |
== | equal | x == y |
!= | not equal | x != y |
> | greater than | x > y |
< | lesser than | x < y |
<= | lesser than or equal to | x <= y |
>= | greater than or equal to | x >= y |
Logical Operators.
This operators are used in combining conditional statements.
Operator | Description | Example |
and | returns true if both of the statements are true | x < 1 and y < 10 |
or | returns true if one of the statements is true | x < 1 or y < 10 |
not | returns false if the result is true | not (x < 1 and y < 10) |
Identity Operators.
This operators compare objects which are similar on the same memory location.
Operator | Description | Example |
is | returns true if both variables are the same object | x is y |
is not | returns true is both variables are not the same object | x is not y |
Membership Operators.
This operators are used to test whether a sequence is present in an object.
Operator | Description | Example |
in | returns true if a sequence with the specified value is present in the object | x in y |
not in | returns true if a sequence with the specified value is not present in the object | x not in y |
And That's all for today guys, keep practicing on this as tomorrow will be looking into lists, tuples, sets and dictionaries.
Exercise: Create a python script that prints your name, age, and your favorite color. ADIOS!
Post a Comment: