The Ultimate Python Tutorial for Beginners
This article contains information about the Python programming language for beginners and intermediates
Table of contents
- What is Python?
- What is Python used for?
- Installation of Python 3
- Running Code
- Syntax
- Variables
- Types
- Determining Type
- Boolean
- Numbers
- Integer
- Float
- String
- Lists
- Initialization
- Empty List
- List with initial values
- Adding to a list
- Updating a List
- Deleting from a List
- Retrieving from a List
- Tuples
- Initialization
- Empty Tuple
- Tuple with initial values
- Adding to a Tuple
- Updating to a Tuple
- Retrieving from a Tuple
- Sets
- Initialization
- Empty Sets
- Set with initial values
- Adding to a set
- Deleting from a set
- Dictionaries
- Dictionary with initial values
- Initialiazation
- Empty Dictionary
- Adding to a Dictionary
- Updating to a Dictionary
- Deleting from a Dictionary
- Retrieving form a dictionary
- Typecasting
- Explicit Conversion
- Implicit conversion
- User Input
- Operators
- while Loops
- Functions
- List Comprehensions
- Modules
- Files
- Classes and Objects
If you are new to programming or an intermediate programmer and want to learn more about Python then start now right here.
We will cover topics of Python in every detail and yet very easy to understand. We believe in learning with examples so you will see lots of examples.
What is Python?
Python is a widely used high-level programming language. It is often compared to Tcl, Perl, Scheme, or Java. However, Python is much more flexible, powerful, and readable than these other systems.
Python is so versatile that it can be used to write a simple computer program to create a complex AI algorithm.
Python is a programming language that lets you work more quickly and integrate your systems more effectively. You can use Python as your base language for your own applications, either interactively in a text editor or non-interactively as a scripting language.
Python is an interpreted language. This means that Python is read as a sequence of characters, and the interpreter does the work of translating the characters into the appropriate actions. This allows Python to be a very powerful language, but also means that Python is not a compiled language. Compiled languages are typically compiled to machine code, which is faster and more efficient than source code.
Python is a free and open-source language.
Python is multi-platform.
Python is cross-platform.
Python supports multiple programming paradigms like Object Oriented, Procedure Functional, etc.
Python was created by Guido Van Rossum and was released in the year 1991.
Python is a **dynamic programming language ** and has an automatic memory management system, which means it requires less rigid coding and hence is a beginner-friendly language.
The motive of python language is to let programmers and developers focus on creating logic rather than worrying about syntax memory management.
Python is very rich in its standard libraries.
What is Python used for?
Python is used in almost all fields of application development such as:
Web and Internet development
Computer Science
Network Programming
Interactive and Dynamic graphical user interfaces
Database applications
Data Analytics
Non-proprietary applications/applications for accounting, business, education, scientific, and other applications
Game development
Data Visualization
Software engineering
Operating systems
Hardware programming
Embedded systems
Application software for business
Application software for education
Application software for scientific
Application software for other applications
Installation of Python 3
Python runs on Mac, Linux, Windows, and many other platforms.
Python had breaking changes between versions 2 and 3. And since Python 2 support ended in 2020, this article is solely based on Python 3.
If you use a Mac or Linux you already have Python installed. But Windows doesn't come with Python installed by default.
You also might have Python 2, and we are going to use Python 3. So you should check to see if you have Python 3 first.
Type the following in your terminal.
python3 -V
Notice the uppercase V
.
If your result is something similar to 'Python 3.x.y', for instance, Python 3.8.1, then you are ready to go.
If not, follow the next instructions according to your Operating System.
Installing Python 3 on Windows
Go to python.org/downloads.
Download the latest version.
After the download, double-click the installer.
On the first screen, check the box indicating to "Add Python 3.x to PATH" and then click on "Install Now".
Wait for the installation process to finish until the next screen with the message "Setup was successful".
Click on "Close".
Installing Python 3 on Mac
Install XCode from the App Store.
Install the command line tools by running the following in your terminal.
xcode-select --install
I recommend using Homebrew. Go to brew.sh and follow the instructions on the first page to install it.
After installing Homebrew, run the following:
brew update brew install python3
Homebrew already adds Python 3 to the PATH
, so you don't have to do anything else.
Installing Python 3 on Linux
To install using apt
, available in Ubuntu and Debian, enter the following:
sudo apt install python3
Running Code
You can run Python code directly in the terminal as commands or you can save the code in a file with the .py
extension and run the Python file.
Terminal
Running commands directly in the terminal is recommended when you want to run something simple.
Open the command line and type python3
richard_o@DESKTOP-CU67DJ:~$ python3
You should see something like this in your terminal indicating the version (in my case, Python 3.6.9), the operating system (I'm using Linux), and some basic commands to help you.
The >>>
tells us we are in the Python console.
>>>
Python 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
Let's test it by running our first program to perform basic math and add two numbers.
>>> 2 + 2
The output is:
4
To exit the Python console simply type exit()
.
>>> exit()
Running .py
files
If you have a complex program, with many lines of code, the Python console isn't the best option.
The alternative is simply to open a text editor, type the code, and save the file with a .py
extension.
Let's do that, create a file called first_
program.py
with the following content.
print('First Program')
The print()
function prints a message on the screen.
The message goes inside the parentheses with either single quotes or double quotes, both work the same.
To run the program, on your terminal do the following:
richard_o@DESKTOP-CU67DJ:~$ python3 first_program.py
The output is:
First Program
Syntax
Python is known for its clean syntax.
The language avoids using unnecessary characters to indicate some specificity.
Semicolons
Python doesn't use semicolons to finish lines. A new line is enough to tell the interpreter that a new command is beginning.
The print()
method will display something.
In this example, we have two commands that will display the messages inside the single quotes.
print('First command')
print('Second command')
Output:
First command
Second command
But the following is wrong due to the semicolons at the end:
print('First command'); print('Second command');
Indentation
Many languages use curly brackets to define scope.
Python's interpreter uses only indentation to define when a scope ends and another one starts.
This means you have to be aware of white spaces at the beginning of each line -- they have meaning and might break your code if misplaced.
This definition of a function works:
def my_function():
print('First command')
This doesn't work because the indentation of the second line is missing and will throw an error:
def my_function():
print('First command')
Case sensitivity and variables
Python is case-sensitive. So the variables name
and Name
are not the same thing and store different values.
name = 'Richard'
Name = 'Orido'
As you can see, variables are easily created by just assigning values to them using the =
symbol.
This means name
stores 'Renan' and Name
stores 'Moura'.
Comments
Finally, to comment on something in your code, use the hash mark #
.
The commented part does not influence the program flow.
# this function prints something
def my_function():
print('First command')
The purpose of comments is to explain what is happening in the code.
If you want others to fully understand your reasoning, good code design is mandatory, but comments are an integral part of any codebase.
Multiline comments in Python
Maybe you want to comment on something very complex or describe how some process works in your code.
In these cases, you can use multiline comments.
To do that, just use a single hash mark #
for each line.
# Everything after the hash mark # is a comment
# This is a comment and it won't influence my program flow
# Calculates the cost of the project given variables a and b
# a is the time in months it will take until the project is finished
# b is how much money it will cost per month a + b * 10
Or make use of multi-line string as a comment.
Python multi-line comment is a piece of text enclosed in a delimiter (""") on each end of the comment. Again there should be no white space between delimiter ("""). They are useful when the comment text does not fit into one line; therefore needs to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code.
""" Multi-line comment used print("Python Comments") """
print("Mathematics")
Variables
In any program, you need to store and manipulate data to create a flow or some specific logic.
That's what variables are for.
You can have a variable to store a name, another one to store the age of a person, or even use a more complex type to store all of this at once like a dictionary.
Creating/Declaring Variables
Declaring a variable is a basic and straightforward operation in Python
Just pick a name and attribute a value to it using the = symbol.
name = 'Tom'
age = 99
You can use the print()
function to show the value of a variable.
print(name)
print(age)
Tom
99
Notice that in Python there is no special word to declare a variable.
The moment you assign a value, the variable is created in memory.
Python also has dynamic typing, which means you don't have to tell it if your variable is a text or a number, for instance.
The interpreter infers the typing based on the value assigned.
If you need it, you can also re-declare a variable just by changing its value.
#declaring name as a string
name='Bob'
#re-declaring name as an int
name = 32
Naming Conventions
Let's continue from the last section when I talked about meaning and context.
Don't use random variable names like x
or y
.
Say you want to store the time of a party, just call it party_time
.
Oh, did you notice the underscore _
?
By convention, if you want to use a variable name that is composed of two or more words, you separate them by underscores. This is called snakecase.
Another option would be using camelCase as in partyTime
. This is very common in other languages, but not the convention in Python as stated before.
Variables are case sensitive, so party_time
and Party_time
are not the same. Also, keep in mind that the convention tells us to always use lower case.
Variable names:
Are Case sensitive:
time
andTIME
are not the sameHave to start with an underscore _ or a letter (DO NOT start with a number)
Are allowed to have only numbers, letters and underscores. No special characters like: #, $, &, @, etc.
Types
To store data in Python you need to use a variable. And every variable has its type depending on the value of the data stored.
Python has dynamic typing, which means you don't have to explicitly declare the type of your variable -- but if you want to, you can.
Determining Type
First of all, let's learn how to determine the data type.
Just use the type()
function and pass the variable of your choice as an argument, like the example below.
print(type(my_variable))
Boolean
The boolean type is one of the most basic types of programming.
A boolean type variable can only represent either True or False.
my_bool = True
print(type(my_bool))
my_bool = bool(1024)
print(type(my_bool))
<class 'bool'>
<class 'bool'>
Numbers
We will focus on two types of numeric types: int and float
Integer
my_int = 32
print(type(my_int))
my_int = int(32)
print(type(my_int))
<class 'int'>
<class 'int'>
Float
my_float = 32.85
print(type(my_float))
my_float = float(32.85)
print(type(my_float))
<class 'float'>
<class 'float'>
String
The text type is one of the most commons types out there and is often called string or, in Python, just str
.
my_city = "New York"
print(type(my_city))
#Single quotes have exactly the same use as double quotes
my_city = 'New York'
print(type(my_city))
#Setting the variable type explicitly
my_city = str("New York")
print(type(my_city))
<class 'str'>
<class 'str'>
<class 'str'>
You can use the + operator to concatenate strings.
Concatenation is when you have two or more strings and you want to join them into one.
word1 = 'New '
word2 = 'York'
print(word1 + word2)
New York
The string type has many built-in methods that let us manipulate them. I will demonstrate how some of these methods work.
The len()
function returns the length of a string.
print(len('New York'))
8
The replace()
method replaces a part of the string with another. As an example, let's replace 'New' for 'Old'.
print('New York'.replace('New', 'Old'))
Old York
The upper()
method will return all characters as uppercase.
print('New York'.upper())
NEW YORK
The lower()
method does the opposite, and returns all characters as lowercase.
print('New York'.lower())
new york
Lists
A list has its items ordered and you can add the same item as many times as you want. An important detail is that lists are mutable.
Mutability means you can change a list after its creation by adding items, removing them, or even just changing their values.
Initialization
Empty List
people = []
List with initial values
my_list = ["bmw", "ferrari", "mclaren"]
print(type(my_list))
my_list = list(("bmw", "ferrari", "maclaren"))
print(type(my_list))
<class 'list'>
<class 'list'>
Adding to a list
To add an item in the end of a list, use append().
people = ['Bob', 'Mary']
people.append('Sarah')
print(people)
['Bob', 'Mary', 'Sarah']
To specify the position for the new item, use the insert()
method.
people = ['Bob', 'Mary']
people.insert(0, 'Sarah')
print(people)
['Sarah', 'Bob', 'Mary']
Updating a List
Specify the position of the item to update and set the new value
people = ['Bob', 'Mary']
people[1] = 'Sarah'
print(people)
['Bob', 'Sarah']
Deleting from a List
Use the remove()
method to delete the item given as an argument.
people = ['Bob', 'Mary']
people.remove('Bob')
print(people)
['Mary']
To delete everybody, use the clear()
method:
people = ['Bob', 'Mary']
people.clear()
Retrieving from a List
Use the index to reference the item.
Remember that the index starts at 0.
So to access the second item use the index 1.
people = ['Bob', 'Mary']
print(people[1])
Mary
Tuples
A tuple is just like a list: ordered, and allows repetition of items.
There is just one difference: a tuple is immutable.
Immutability means you can't change a tuple after its creation. If you try to add an item or update one, for instance, the Python intepreter will show you an error. I will show that these errors occur later in the section dedicated to Tuples.
Initialization
Empty Tuple
people = ()
Tuple with initial values
my_tuple = ("bmw", "ferrari", "mclaren")
print(type(my_tuple))
my_tuple = tuple(("bmw", "ferrari", "mclaren"))
print(type(my_tuple))
<class 'tuple'>
<class 'tuple'>
Adding to a Tuple
Tuples are immutable. This means that if you try to add an item, you will see an error.
people = ('Bob', 'Mary')
people[2] = 'Sarah'
Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment
Updating to a Tuple
Updating an item will also return an error.
But there is a trick: you can convert it into a list, change the item, and then convert it back to a tuple.
people = ('Bob', 'Mary')
people_list = list(people)
people_list[1] = 'Sarah'
people = tuple(people_list)
print(people)
('Bob', 'Sarah')
For the same reason you can't add an item, you also can't delete an item, since they are immutable.
Retrieving from a Tuple
Use the index to reference the item.
people = ('Bob', 'Mary')
print(people[1])
Mary
Sets
Sets don't guarantee the order of the items and are not indexed.
A key point when using sets: they don't allow repetition of an item.
my_set = {"bmw", "ferrari", "mclaren"}
print(type(my_set))
my_set = set(("bmw", "ferrari", "mclaren"))
print(type(my_set))
<class 'set'>
<class 'set'>
Initialization
Empty Sets
people = set()
Set with initial values
people = {'Bob', 'Mary'}
Adding to a set
Use the add()
method to add one item.
people.add('Sarah')
Use the update()
method to add multiple items at once.
people.update(['Carol', 'Susan'])
Remember, Sets do not allow repetition, so if you add 'Mary' again, nothing changes.
people = {'Bob', 'Mary'}
people.add('Mary')
print(people)
{'Bob', 'Mary'}
Items in a set are not mutable. You have to either add or delete an item.
Deleting from a set
To remove Bob from the dictionary:
people = {'Bob', 'Mary'}
people.remove('Bob')
print(people)
{'Mary'}
To delete everybody:
people.clear()
Dictionaries
A dictionary doesn't guarantee the order of the elements and is mutable.
One important characteristic in dictionaries is that you can set your own access keys for each element.
Dictionary with initial values
my_dict = {"country": "France", "worldcups": 2}
print(type(my_dict))
my_dict = dict(country="France", worldcups=2)
print(type(my_dict))
<class 'dict'>
<class 'dict'>
Initialiazation
Empty Dictionary
people = {}
Adding to a Dictionary
If the key doesn't exist yet, it is appended to the dictionary.
people['Sarah']=32
Updating to a Dictionary
If the key already exists, the value is just updated.
#Bob's age is 28 now
people['Bob']=28
Notice that the code is pretty much the same.
Deleting from a Dictionary
To remove Bob
from the dictionary:
people.pop('Bob')
To delete everybody:
people.clear()
Retrieving form a dictionary
bob_age = people['Bob']
print(bob_age)
30
Typecasting
Typecasting allows you to convert between different types.
This way you can have an int
turned into a str
, or a float
turned into an int
, for instance.
Explicit Conversion
To cast a variable to a string just use the str()
function.
# This is just a regular explicit initialization
my_str = str('32')
print(my_str)
# int to str
my_str = str(32)
print(my_str)
# float to str
my_str = str(32.0)
print(my_str)
32
32
32.0
To cast a variable to an integer just use the int()
function.
# This is just a regular explicit initialization
my_int = int(32)
print(my_int)
# float to int: rounds down to 3
my_int = int(3.2)
print(my_int)
# str to int
my_int = int('32')
print(my_int)
32
3.2
32
To cast a variable to a float just use the float()
function.
# This is an explicit initialization
my_float = float(3.2)
print(my_float)
# int to float
my_float = float(32)
print(my_float)
# str to float
my_float = float('32')
print(my_float)
3.2
32.0
32.0
What I did above is called an explicit type conversion.
In some cases, you don't need to do the conversion explicitly, since Python can do it by itself.
Implicit conversion
The example below shows implicit conversion when adding an int
and a float
.
Notice that my_sum
is float
. Python uses float
to avoid data loss since the int
type cannot represent the decimal digits.
my_int = 32
my_float = 3.2
my_sum = my_int + my_float
print(my_sum)
print(type(my_sum))
35.2
<class 'float'>
On the other hand, in this example, when you add an int
and a str
, Python will not be able to make the implicit conversion, and the explicit type conversion is necessary.
my_int = 32
my_str = '32'
# explicit conversion works
my_sum = my_int + int(my_str)
print(my_sum)
#implicit conversion throws an error my_sum = my_int + my_str
64
Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str'
The same error is thrown when trying to add float
and str
types without making an explicit conversion.
my_float = 3.2
my_str = '32'
# explicit conversion works
my_sum = my_float + float(my_str)
print(my_sum)
#implicit conversion throws an error my_sum = my_float + my_str
35.2
Traceback (most recent call last): File "", line 1, in TypeError: unsupported operand type(s) for +: 'float' and 'str'
User Input
If you need to interact with a user when running your program in the command line (for example, to ask for a piece of information), you can use the input()
function.
country = input("What is your country? ") #user enters 'Brazil'
print(country)
Brazil
The captured value is always string
. Just remember that you might need to convert it using typecasting.
age = input("How old are you? ") #user enters '29'
print(age)
print(type(age))
age = int(age)
print(type(age))
The output for each print()
is:
29
<class 'str'>
<class 'int'>
Notice the age 29 is captured as string
and then converted explicitly to int
.
Operators
In a programming language, operators are special symbols that you can apply to your variables and values in order to perform operations such as arithmetic/mathematical and comparison.
Python has lots of operators that you can apply to your variables and I will demonstrate the most used ones.
Arithmetic Operators
Arithmetic operators are the most common type of operators and also the most recognizable ones.
They allow you to perform simple mathematical operations.
They are:
+
: Addition-
:Subtraction*
: Multiplication/
: Division**
: Exponentiation//
: Floor Division, rounds down the result of a division%
: Modulus, gives you the remainder of a division Let's see a program that shows how each of them is used.
print('Addition:', 5 + 2)
print('Subtraction:', 5 - 2)
print('Multiplication:', 5 * 2)
print('Division:', 5 / 2)
print('Floor Division:', 5 // 2)
print('Exponentiation:', 5 ** 2)
print('Modulus:', 5 % 2)
Addition: 7
Subtraction: 3
Multiplication: 10
Division: 2.5
Floor Division: 2
Exponentiation: 25
Modulus: 1
Concatenation
Concatenation is when you have two or more strings and you want to join them into one.
This useful when you have information in multiple variables and want to combine them.
For instance, in this next example, I combine two variables that contain my first name and my last name respectively to have my full name.
The +
operator is also used to concatenate.
first_name = 'Richard'
last_name = 'Orido'
print(first_name + last_name)
Richard Orido
Since concatenation is applied to strings, to concatenate strings with other types, you have to do an explicit typecast using str()
.
I have to typecast the int
value 30 to string with str()
to concatenate it with the rest of the string.
age = 'I am ' + str(30) + ' years old'
print(age)
I am 30 years old
Comparison Operators
Use comparison operators to compare two values.
These operators return either True
or False
.
They are:
==
: Equal
!=
: Not equal
>
: Greater than
<
: Less than
>=
: Greater than or equal to
<=
: Less than or equal to
Let's see a program that shows how each of them is used.
print('Equal:', 5 == 2)
print('Not equal:', 5 != 2)
print('Greater than:', 5 > 2)
print('Less than:', 5 < 2)
print('Greater than or equal to:', 5 >= 2)
print('Less than or equal to:', 5 <= 2)
Equal: False
Not equal: True
Greater than: True
Less than: False
Greater than or equal to: True
Less than or equal to: False
Assignment Operators
As the name implies, these operators are used to assign values to variables.
x = 7
in the first example is a direct assignment storing the number 7
in the variable x
.
The assignment operation takes the value on the right and assigns it to the variable on the left.
The other operators are simple shorthands for the Arithmetic Operators.
In the second example x
starts with 7
and x += 2
is just another way to write x = x + 2
. This means the previous value of x
is added by 2
and reassigned to x
that is now equal to 9
.
=
: simple assignment
x = 7
print(x)
7
+=
: addition and assignment
x = 7
x += 2
print(x)
9
-=
: subtraction and assignment
x = 7
x -= 2
print(x)
5
*=
: multiplication and assignment
`*=`: multiplication and assignment
x = 7
x *= 2
print(x)
14
/=
: division and assignment
x = 7
x /= 2
print(x)
3.5
%=
: modulus and assignment
x = 7
x %= 2
print(x)
1
//=: floor division and assignment
x = 7
x //= 2
print(x)
3
**=
: exponentiation and assignment
x = 7
x **= 2
print(x)
49
Logical Operators
Logical operators are used to combine statements applying boolean algebra.
They are:
and
: True
only when both statements are true or
: False
only when both x and y are false not
: The not
operator simply inverts the input, True
becomes False
and vice versa. Let's see a program that shows how each one is used.
x = 5
y = 2
print(x == 5 and y > 3)
print(x == 5 or y > 3)
print(not (x == 5))
False
True
False
Membership Operators
These operators provide an easy way to check if a certain object is present in a sequence: string
, list
, tuple
, set
, and dictionary
.
They are:
in
: returns True
if the object is present not in
: returns True
if the object is not present Let's see a program that shows how each one is used.
number_list = [1, 2, 4, 5, 6]
print( 1 in number_list)
print( 5 not in number_list)
print( 3 not in number_list)
True
False
True
Conditionals
Conditionals are one of the cornerstones of any programming language.
They allow you to control the program flow according to specific conditions you can check.
The if
statement
The way you implement a conditional is through the if
statement.
The general form of an if
statement is:
if expression: statement
The expression
contains some logic that returns a boolean, and the statement
is executed only if the return is True
.
A simple example:
bob_age = 32
sarah_age = 29
if bob_age > sarah_age:
print('Bob is older than Sarah')
Bob is older than Sarah
We have two variables indicating the ages of Bob and Sarah. The condition in plain English says "if Bob's age is greater than Sarah's age, then print the phrase 'Bob is older than Sarah'".
Since the condition returns True
, the phrase will be printed on the console.
The if else
and elif
statements
In our last example, the program only does something if the condition returns True
.
But we also want it to do something if it returns False
or even check a second or third condition if the first one wasn't met.
In this example, we swapped Bob's and Sarah's age. The first condition will return False
since Sarah is older now, and then the program will print the phrase after the else
instead.
bob_age = 29
sarah_age = 32
if bob_age > sarah_age:
print('Bob is older than Sarah')
else:
print('Bob is younger than Sarah')
Bob is younger than Sarah
Now, consider the example below with the elif
.
bob_age = 32
sarah_age = 32
if bob_age > sarah_age:
print('Bob is older than Sarah')
elif bob_age == sarah_age:
print('Bob and Sarah have the same age')
else:
print('Bob is younger than Sarah')
Bob and Sarah have the same age
The purpose of the elif
is to provide a new condition to be checked before the else
is executed.
Once again, we changed their ages and now both are 32 years old.
As such, the condition in the elif
is met. Since both have the same age, the program will print "Bob and Sarah have the same age".
Notice you can have as many elifs
as you want, just put them in sequence.
bob_age = 32
sarah_age = 32
if bob_age > sarah_age:
print('Bob is older than Sarah')
elif bob_age < sarah_age:
print('Bob is younger than Sarah')
elif bob_age == sarah_age:
print('Bob and Sarah have the same age')
else:
print('This one is never executed')
Bob and Sarah have the same age
In this example, the else
is never executed because all the possibilities are covered in the previous conditions and thus could be removed.
Nested Conditionals
You might need to check more than one conditional for something to happen.
In this case, you can nest your if
statements.
For instance, the second phrase "Bob is the oldest" is printed only if both if
s pass.
bob_age = 32
sarah_age = 28
mary_age = 25
if bob_age > sarah_age:
print('Bob is older than Sarah')
if bob_age > mary_age:
print('Bob is the oldest')
Bob is older than Sarah Bob is the oldest
Or, depending on the logic, make it simpler with Boolean Algebra.
This way, your code is smaller, more readable and easier to maintain.
bob_age = 32
sarah_age = 28
mary_age = 25
if bob_age > sarah_age and bob_age > mary_age:
print('Bob is the oldest')
Bob is the oldest
Ternary Operators
The ternary operator is a one-line if
statement.
It's very handy for simple conditions.
This is how it looks:
if else
Consider the following Python code:
a = 25, b = 50, x = 0, y = 1
result = x if a > b else y
print(result)
1
Here we use four variables, a
and b
are for the condition, while x
and y
represent the expressions.
a
and b
are the values we are checking against each other to evaluate some condition. In this case, we are checking if a
is greater than b
.
If the expression holds true, i.e., a
is greater than b
, then the value of x
will be attributed to result
which will be equal to 0.
However, if a
is less than b
, then we have the value of y
assigned to result
, and result
will hold the value 1
.
Since a
is less than b
, 25 < 50, result
will have 1
as final value from y
.
The easy way to remember how the condition is evaluated is to read it in plain English.
Our example would read: result
will be x
if a
is greater than b
otherwise y
.
while
Loops
Loops are used when you need to repeat a block of code a certain number of times or apply the same logic over each item in a collection.
There are two types of loops: for
and while
.
You will learn about for loops in the next section.
Basic Syntax
The basic syntax of a while loop is as below.
while condition: statement
The loop will continue while the condition is True
.
Example:
number = 1
while number <= 5:
print(number, 'squared is', number**2)
number = number + 1
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
You can use any variable name, but I chose number
because it makes sense in the context. A common generic choice would be simply i
.
The loop will go on until number
(initialized with 1) is less than or equal to 5.
Notice that after the print()
command, the variable number
is incremented by 1 to take the next value.
If you don't do the incrementation you will have an infinite loop since number
will never reach a value greater than 5. This is very important.
else
block
When the condition returns False
, the else
block will be called.
number = 1
while number <= 5:
print(number, 'squared is', number**2)
number = number + 1
else:
print('No numbers left!')
1 squared is 1
2 squared is 4
3 squared is 9
4 squared is 16
5 squared is 25
No numbers left!
Notice the phrase 'No numbers left!' is printed after the loop ends, that is after the condition number <= 5
evaluates to False
.
Simply use the break
keyword, and the loop will stop its execution.
number = 1
while number <= 5:
print(number, 'squared is', number**2)
number = number + 1
if number == 4:
break
1 squared is 1
2 squared is 4
3 squared is 9
The loop runs normally, and when number
reaches 4 the if
statement evaluates to True
and the break
command is called. This finishes the loop before the squared value of the numbers 4 and 5 are calculated.
for
Loops
for
loops are similar to while
loops in the sense that they are used to repeat blocks of code.
The most important difference is that you can easily iterate over sequential types.
Basic Syntax
The basic syntax of a for
loop is as below.
for item in collection: statement
Loop over a list
To loop over a list or any other collection, just proceed as shown in the example below.
cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
print(car)
BMW
Ferrari
McLaren
The list of cars
contains three items. The for
loop will iterate over the list and store each item in the car
variable, and then execute a statement, in this case print(car)
, to print each car in the console.
range()
function
The range function is widely used for loops because it gives you a simple way to list numbers.
This code will loop through the numbers 0 to 5 and print each of them.
for number in range(5):
print(number)
0 1 2 3 4
You can also define a start
and stop
using range()
.
Here we are starting in 5 and stopping in 10. The number you set to stop is not included.
for number in range(5, 10):
print(number)
5
6
7
8
9
Finally, it is also possible to set a step.
Here we start with 10 and increment by 2 until 20, since 20 is the stop
, it is not included.
for number in range(10, 20, 2):
print(number)
10
12
14
16
18
else
block
When the items in the list are over, the else
block will be called.
cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
print(car)
else:
print('No cars left!')
BMW
Ferrari
McLaren
No cars left!
Simply use the break
keyword, and the loop will stop its execution.
cars = ['BMW', 'Ferrari', 'McLaren']
for car in cars:
print(car)
if car == 'Ferrari':
break
BMW
Ferrari
The loop will iterate through the list and print each car.
In this case, after the loop reaches 'Ferrari', the break
is called and 'McLaren' won't be printed.
Nested Loops
Sometimes you have more complex collections, like a list of lists.
To iterate over these lists, you need nested for
loops.
In this case, I have three lists: one for BMW models, another for Ferrari models, and finally one with McLaren models.
The first loop iterates over each brand's list, and the second will iterate over the models of each brand.
car_models = [ ['BMW I8', 'BMW X3', 'BMW X1'], ['Ferrari 812', 'Ferrari F8', 'Ferrari GTC4'], ['McLaren 570S', 'McLaren 570GT', 'McLaren 720S'] ]
for brand in car_models:
for model in brand:
print(model)
BMW I8 BMW X3 BMW X1
Ferrari 812 Ferrari F8 Ferrari GTC4
McLaren 570S McLaren 570GT McLaren 720S
Loop over a Tuple
people = ('Bob', 'Mary')
for person in people:
print(person)
Bob
Mary
Loop over a set
people = {'Bob', 'Mary'}
for person in people:
print(person)
Bob
Mary
Loop over a Dictionary
To print the keys:
people = {'Bob':30, 'Mary':25}
for person in people:
print(person)
Bob
Mary
To print the values:
people = {'Bob':30, 'Mary':25}
for person in people:
print(people[person])
30
25
Functions
As the code grows the complexity also grows. And functions help organize the code.
Functions are a handy way to create blocks of code that you can reuse.
Definition and Calling
In Python use the def
keyword to define a function.
Give it a name and use parentheses to inform 0 or more arguments.
In the line after the declaration starts, remember to indent the block of code.
Here is an example of a function called print_first_function()
that only prints a phrase 'My first function!'.
To call the function just use its name as defined.
def print_first_function():
print('My first function!')
print_first_function()
My first function!
return
a value
Use the return
keyword to return a value from the function.
In this example the function second_function()
returns the string 'My second function!'.
Notice that print()
is a built-in function and our function is called from inside it.
The string returned by second_function()
is passed as an argument to the print()
function.
def second_function():
return 'My second function!'
print(second_function())
My second function!
Arguments
You can define parameters between the parentheses.
When calling a function with parameters you have to pass arguments according to the parameters defined.
The past examples had no parameters, so there was no need for arguments. The parentheses remained empty when the functions were called.
One Argument
To specify one parameter, just define it inside the parentheses.
In this example, the function my_number
expects one number as argument defined by the parameter num.
The value of the argument is then accessible inside the function to be used.
def my_number(num):
return 'My number is: ' + str(num)
print(my_number(10))
My number is: 10
Two or more Arguments
To define more parameters, just use a comma to separate them.
Here we have a function that adds two numbers called add
. It expects two arguments defined by first_num
and second_num
.
The arguments are added by the +
operator and the result is then returned by the return
.
def add(first_num, second_num):
return first_num + second_num
print(add(10,2))
12
This example is very similar to the last one. The only difference is that we have 3 parameters instead of 2.
def add(first_num, second_num, third_num):
return first_num + second_num + third_num
print(add(10,2,3))
15
This logic of defining parameters and passing arguments is the same for any number of parameters.
It is important to point out that the arguments have to be passed in the same order that the parameters are defined.
Default value.
You can set a default value for a parameter if no argument is given using the =
operator and a value of choice.
In this function, if no argument is given, the number 30 is assumed as the expected value by default.
def my_number(my_number = 30):
return 'My number is: ' + str(my_number)
print(my_number(10))
print(my_number())
My number is: 10
My number is: 30
Keyword or Named Arguments
When calling a function, the order of the arguments have to match the order of the parameters.
The alternative is if you use keyword or named arguments.
Set the arguments to their respective parameters directly using the name of the parameters and the =
operators.
This example flips the arguments, but the function works as expected because I tell it which value goes to which parameter by name.
def my_numbers(first_number, second_number):
return 'The numbers are: ' + str(first_number) + ' and ' + str(second_number)
print(my_numbers(second_number=30, first_number=10))
The numbers are: 10 and 30
Any number of arguments: *args
If you don't want to specify the number of parameters, just use the *
before the parameter name. Then the function will take as many arguments as necessary.
The parameter name could be anything like *numbers
, but there is a convention in Python to use *args
for this definition of a variable number of arguments.
def my_numbers(*args):
for arg in args:
print(number)
my_numbers(10,2,3)
10 2 3
Any number of Keyword/Named arguments: **kwargs
Similar to *args
, we can use **kwargs
to pass as many keyword arguments as we want, as long as we use **
.
Again, the name could be anything like **numbers
, but **kwargs
is a convention.
def my_numbers(**kwargs):
for key, value in kwargs.items():
print(key)
print(value)
my_numbers(first_number=30, second_number=10)
first_number
30
second_number
10
Other types as arguments
The past examples used mainly numbers, but you can pass any type as argument and they will be treated as such inside the function.
This example takes strings as arguments.
def my_sport(sport):
print('I like ' + sport)
my_sport('football')
my_sport('swimming')
I like football
I like swimming
This function takes a list as an argument.
def my_numbers(numbers):
for number in numbers:
print(number)
my_numbers([30, 10, 64, 92, 105])
30
10
64
92
105
Scope
The place where a variable is created defines its availability to be accessed and manipulated by the rest of the code. This is known as scope.
There are two types of scope: local and global.
Global Scope
A global scope allows you to use the variable anywhere in your program.
If your variable is outside a function, it has global scope by default.
name = 'Bob'
def print_name():
print('My name is ' + name)
print_name()
My name is Bob
Notice that the function could use the variable name
and print My name is Bob
.
Local Scope
When you declare a variable inside a function, it only exists inside that function and can't be accessed from the outside.
def print_name():
name = "Bob" print('My name is ' + name)
print_name()
My name is Bob
The variable name
was declared inside the function, so the output is the same as before.
But this will throw an error:
def print_name():
name = 'Bob' print('My name is ' + name)
print(name)
The output of the code above is:
Traceback (most recent call last): File "", line 1, in NameError: name 'name' is not defined
We tried to print the variable name from outside the function, but the scope of the variable was local and could not be found in a global scope.
Mixing Scopes
If you use the same name for variables inside and outside a function, the function will use the one inside its scope.
So when you call print_name()
, the name='Bob'
is used to print the phrase.
On the other hand, when calling print()
outside the function scope, name="Sarah"
is used because of its global scope.
name = "Sarah"
def print_name():
name = 'Bob' print('My name is ' + name)
print_name()
print(name)
The output of the above code is:
My name is Bob
Sarah
List Comprehensions
Sometimes we want to perform some very simple operations over the items of a list.
List comprehensions give us a succinct way to work on lists as an alternative to other methods of iteration, such as for
loops.
Basic syntax
To use a list comprehension to replace a regular for loop, we can make:
[expression for item in list]
which is the same as doing:
for item in list: expression
If we want some conditional to apply the expression, we have:
[expression for item in list if conditional ]
which is the same as doing:
for item in list: if conditional: expression
Example : calculating the cube of a number
Regular way
numbers = [1, 2, 3, 4, 5]
new_list = []
for n in numbers:
new_list.append(n**3)
print(new_list)
1, 8, 27, 64, 125
Using list comprehensions
numbers = [1, 2, 3, 4, 5]
new_list = []
new_list = [n**3 for n in numbers]
print(new_list)
1, 8, 27, 64, 125
Modules
After some time your code starts to get more complex with lots of functions and variables.
To make it easier to organize the code we use Modules.
A well-designed Module also has the advantage of being reusable, so you write code once and reuse it everywhere.
You can write a module with all the mathematical operations and other people can use it.
And, if you need, you can use someone else's modules to simplify your code, speeding up your project.
In other programming languages, these are also referred to as libraries.
Using a Module
To use a module we use the import
keyword.
As the name implies we have to tell our program what module to import.
After that, we can use any function available in that module.
Let's see an example using the math
module.
First, let's see how to have access to a constant, Euler's number.
import math
math.e
2.718281828459045
In this second example, we are going to use a function that calculates the square root of a number.
It is also possible to use the as
keyword to create an alias.
import math as m
m.sqrt(121)
m.sqrt(729)
11
27
Finally, using the from
keyword, we can specify exactly what to import instead of the whole module and use the function directly without the module's name.
This example uses the floor()
function that returns the largest integer less than or equal to a given number.
from math import floor
floor(9.8923)
9
Creating a Module
Now that we know how to use modules, let's see how to create one.
It is going to be a module with the basic math operations add
, subtract
, multiply
, divide
and it is gonna be called basic_operations
.
Create the basic_
operations.py
file with the four functions.
def add(first_num, second_num): return first_num + second_num
def subtract(first_num, second_num): return first_num - second_num
def multiply(first_num, second_num): return first_num * second_num
def divide(first_num, second_num): return first_num / second_num
Then, just import the basic_operations
module and use the functions.
import basic_operations
basic_operations.add(10,2)
basic_operations.subtract(10,2)
basic_operations.multiply(10,2)
basic_operations.divide(10,2)
12
8
20
5.0
Files
Creating, deleting, reading, and many other functions applied to files are an integral part of many programs.
As such, it is very important to know how to organize and deal with files directly from your code.
Let's see how to handle files in Python.
File create
First things first, create!
We are going to use the open()
function.
This function opens a file and returns its corresponding object.
The first argument is the name of the file we are handling, the second refers to the operation we are using.
The code below creates the file "people.txt", the x
argument is used when we just want to create the file. If a file with the same name already exists, it will throw an exception.
people_file = open("people.txt", "x")
You can also use the w
mode to create a file. Unlike the x
mode, it will not throw an exception since this mode indicates the writing mode. We are opening a file to write data into it and, if the file doesn't exist, it is created.
people_file = open("people.txt", "w")
The last one is the a
mode which stands for append. As the name implies, you can append more data to the file, while the w
mode simply overwrites any existing data.
When appending, if the file doesn't exist, it also creates it.
people_file = open("people.txt", "a")
File write
To write data into a file, you simply open a file with the w
mode.
Then, to add data, you use the object returned by the open()
function. In this case, the object is called people_file
. Then you call the write()
function passing the data as an argument.
people_file = open("people.txt", "w")
people_file.write("Bob\n")
people_file.write("Mary\n")
people_file.write("Sarah\n")
people_file.close()
We use \n
at the end to break the line, otherwise, the content in the file will stay in the same line as "BobMarySarah".
One more detail is to close() the file. This is not only a good practice but also ensures that your changes were applied to the file.
Remember that when using w
mode, the data that already existed in the file will be overwritten by the new data. To add new data without losing what was already there, we have to use the append mode.
File append
The a
mode appends new data to the file, keeping the existing one.
In this example, after the first writing with w
mode, we are using the a
mode to append. The result is that each name will appear twice in the file "people.txt".
#first write
people_file = open("people.txt", "w")
people_file.write("Bob\n")
people_file.write("Mary\n")
people_file.write("Sarah\n")
people_file.close()
#appending more data
#keeping the existing data
people_file = open("people.txt", "a")
people_file.write("Bob\n")
people_file.write("Mary\n")
people_file.write("Sarah\n")
people_file.close()
File read
Reading the file is also very straightforward: just use the r
mode like so.
If you read the "people.txt" file created in the last example, you should see 6 names in your output.
people_file = open("people.txt", "r")
print(people_file.read())
Bob
Mary
Sarah
Bob
Mary
Sarah
The read()
function reads the whole file at once. If you use the readline()
function, you can read the file line by line.
people_file = open("people.txt", "r")
print(people_file.readline())
print(people_file.readline())
print(people_file.readline())
Bob
Mary
Sarah
You can also loop to read the lines like the example below.
people_file = open("people.txt", "r")
for person in people_file:
print(person)
Bob
Mary
Sarah
Bob
Mary
Sarah
To delete a file, you also need the os
module.
Use the remove()
method.
import os
os.remove('my_file.txt')
Use the os.path.exists()
method to check the existence of a file.
import os
if os.path.exists('my_file.txt'): os.remove('my_file.txt') else: print('There is no such file!')
Classes and Objects
Classes and Objects are the fundamental concepts of Object-Oriented Programming.
In Python, everything is an object!
A variable (object) is just an instance of its type (class).
That's why when you check the type of a variable you can see the class
keyword right next to its type (class).
This code snippet shows that my_city
is an object and it is an instance of the class str
.
my_city = "New York" print(type(my_city))
<class 'str'>
Differentiate Class x Object
The class gives you a standard way to create objects. A class is like a base project.
Say you are an engineer working for Boeing.
Your new mission is to build the new product for the company, a new model called 747-Space. This aircraft flies higher altitudes than other commercial models.
Boeing needs to build dozens of those to sell to airlines all over the world, and the aircrafts have to be all the same.
To guarantee that the aircrafts (objects) follow the same standards, you need to have a project (class) that can be replicable.
The class is a project, a blueprint for an object.
This way you make the project once, and reuse it many times.
In our code example before, consider that every string has the same behavior and the same attributes. So it only makes sense for strings to have a class str
to define them.
Attributes and Methods
Objects have some behavior which is is given by attributes and methods.
In simple terms, in the context of an object, attributes are variables and methods are functions attached to an object.
For example, a string has many built-in methods that we can use.
They work like functions, you just need to separate them from the objects using a .
.
In this code snippet, I'm calling the replace()
method from the string variable my_city
which is an object, and an instance of the class str
.
The replace()
method replaces a part of the string for another and returns a new string with the change. The original string remains the same.
Let's replace 'New' for 'Old' in 'New York'.
my_city = 'New York' print(my_city.replace('New', 'Old'))
print(my_city)
2
Old York
New York
Creating a Class
We have used many objects (instances of classes) like strings, integers, lists, and dictionaries. All of them are instances of predefined classes in Python.
To create our own classes we use the class
keyword.
By convention, the name of the class matches the name of the .py
file and the module by consequence. It is also a good practice to organize the code.
Create a file vehicle.py
with the following class Vehicle
.
class Vehicle:
def init(self, year, model, plate_number, current_speed = 0):
self.year = year
self.model = model
self.plate_number = plate_number
self.current_speed = current_speed
def move(self):
self.current_speed += 1
def accelerate(self, value):
self.current_speed += value
def stop(self):
self.current_speed = 0
def vehicle_details(self):
return self.model + ', ' + str(self.year) + ', ' + self.plate_number
Let's break down the class to explain it in parts.
The class
keyword is used to specify the name of the class Vehicle
.
The __init__
function is a built-in function that all classes have. It is called when an object is created and is often used to initialize the attributes, assigning values to them, similar to what is done to variables.
The first parameter self
in the __init__
function is a reference to the object (instance) itself. We call it self
by convention and it has to be the first parameter in every instance method, as you can see in the other method definitions def move(self)
, def accelerate(self, value)
, def stop(self)
, and def vehicle_details(self)
.
Vehicle
has 5 attributes (including self): year
, model
, plate_number
, and current_speed
.
Inside the __init__
, each one of them is initialized with the parameters given when the object is instantiated.
Notice that current_speed
is initialized with 0
by default, meaning that if no value is given, current_speed
will be equal to 0 when the object is first instantiated.
Finally, we have three methods to manipulate our vehicle regarding its speed: def move(self)
, def accelerate(self, value)
, and def stop(self)
.
And one method to give back information about the vehicle: def vehicle_details(self)
.
The implementation inside the methods work the same way as in functions. You can also have a return
to give you back some value at the end of the method as demonstrated by def vehicle_details(self)
.
Using the Class
To use the class in your terminal, import the Vehicle
class from the vehicle module.
Create an instance called my_car
, initializing year
with 2009, model
with 'F8', plate_number
with 'ABC1234', and current_speed
with 100.
The sel
f parameter is not taken into consideration when calling methods. The Python interpreter infers its value as being the current object/instance automatically, so we just have to pass the other arguments when instantiating and calling methods.
Now use the methods to move()
the car which increases its current_speed
by 1, accelerate(10)
which increases its current_speed
by the value given in the argument, and stop()
which sets the current_speed
to 0
.
Remember to print the value of current_speed
at every command to see the changes.
To finish the test, call vehicle_details()
to print the information about our vehicle.
from vehicle import Vehicle
my_car = Vehicle(2009, 'F8', 'ABC1234', 100)
print(my_car.current_speed)
100
my_car.move()
print(my_car.current_speed)
101
my_car.accelerate(10)
print(my_car.current_speed)
111 my_car.stop()
print(my_car.current_speed)
0
print(my_car.vehicle_details())
F8, 2009, ABC1234
If we don't set the initial value for current_speed
, it will be zero by default as stated before and demonstrated in the next example.
from vehicle import Vehicle
my_car = Vehicle(2009, 'F8', 'ABC1234')
print(my_car.current_speed)
0
my_car.move()
print(my_car.current_speed)
1
my_car.accelerate(10)
print(my_car.current_speed)
11
my_car.stop()
print(my_car.current_speed)
0
print(my_car.vehicle_details())
F8, 2009, ABC1234
Inheritance
Let's define a generic Vehicle
class and save it inside the vehicle.py
file.
class Vehicle:
def init(self, year, model, plate_number, current_speed):
self.year = year
self.model = model
self.plate_number = plate_number
self.current_speed = current_speed
def move(self):
self.current_speed += 1
def accelerate(self, value):
self.current_speed += value
def stop(self):
self.current_speed = 0
def vehicle_details(self):
return self.model + ', ' + str(self.year) + ', ' + self.plate_number
A vehicle has attributes year
, model
, plate_number
, and current_speed
.
The definition of vehicle in the Vehicle
is very generic and might not be suitable for trucks, for instance, because it should include a cargo
attribute.
On the other hand, a cargo attribute does not make much sense for small vehicles like motorcycles.
To solve this we can use inheritance.
When a class (child) inherits another class (parent), all the attributes and methods from the parent class are inherited by the child class.
Parent and Child
In our case, we want a new Truck
class to inherit everything from the Vehicle
class. Then we want it to add its own specific attribute current_cargo
to control the addition and removal of cargo from the truck.
The Truck
class is called a child class that inherits from its parent class Vehicle
.
A parent class is also called a superclass while a child class is also known as a subclass.
Create the class Truck
and save it inside the truck.py
file.
from vehicle import Vehicle
class Truck(Vehicle):
def init(self, year, model, plate_number, current_speed, current_cargo):
super().init(year, model, plate_number, current_speed)
self.current_cargo = current_cargo
def add_cargo(self, cargo):
self.current_cargo += cargo
def remove_cargo(self, cargo):
self.current_cargo -= cargo
Let's break down the class to explain it in parts.
The class Vehicle
inside the parentheses when defining the class Truck
indicates that the parent Vehicle
is being inherited by its child Truck
.
The __init__
method has self as its first parameter, as usual.
The parameters year
, model
, plate_number
, and current_speed
are there to match the ones in the Vehicle
class.
We added a new parameter current_cargo
suited for the Truck
` class.
In the first line of the __init__
method of the Truck
class we have to call the __init__
method of the Vehicle
class.
To do that we use super()
to make a reference to the superclass Vehicle
, so when super().init(year, model, plate_number, current_speed)` is called we avoid repetition of our code.
After that, we can assign the value of current_cargo
normally.
Finally, we have two methods to deal with the current_cargo
: def add_cargo(self, cargo)
:, and def remove_cargo(self, cargo
):
Remember that Truck
inherits attributes and methods from Vehicle
, so we also have an implicit access to the methods that manipulate the speed: def move(self)
, def accelerate(self, value)
, and def stop(self)
.
Using the Truck class
To use the class in your terminal, import the Truck
class from the truck
module.
Create an instance called my_truck
, initializing year
with 2015, model
with 'V8', plate_number
with 'XYZ1234', current_speed
with 0, and current_cargo
with 0.
Use add_cargo(10)
to increase current_cargo
by 10, remove_cargo(4)
, to decrease current_cargo
by 4.
Remember to print the value of current_cargo
at every command to see the changes.
By inheritance, we can use the methods from the Vehicle
class to move()
the truck which increases its current_speed
by 1, accelerate(10)
which increases its current_speed
by the value given in the argument, and stop()
which sets the current_speed
to 0.
Remember to print the value of current_speed
at every interaction to see the changes.
To finish the test, call vehicle_details()
inherited from the Vehicle
class to print the information about our truck.
from truck import Truck
my_truck = Truck(2015, 'V8', 'XYZ1234', 0, 0)
print(my_truck.current_cargo)
0
my_truck.add_cargo(10)
print(my_truck.current_cargo)
10
my_truck.remove_cargo(4)
print(my_truck.current_cargo)
6
print(my_truck.current_speed)
0
my_truck.accelerate(10)
print(my_truck.current_speed)
10
my_truck.stop()
print(my_truck.current_speed)
0
print(my_truck.vehicle_details())
V8, 2015, XYZ1234