Python 3 Tutorial
Python is a popular high-level, interpreted programming language. It is easy to learn and has a variety of applications ranging from web development and scientific computing to artificial intelligence and machine learning. In this tutorial, we will be looking at the basic concepts of Python 3 and how to get started with coding in Python.
Getting started
Before we delve into coding with Python, we need to make sure we have Python 3 installed on our computer. First, we need to check if Python 3 is installed. Open up your terminal or command prompt and type the following command:
python3
If Python 3 is installed, you will see something like this:
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
This means that Python 3 is installed on your computer and you can start coding. However, if you don’t have Python 3 installed, you can download it from the official website at https://www.python.org/downloads/.
Basic concepts
Python is a high-level programming language. This means that it is designed to be easy to read and write. Python syntax is simple and easy to understand. Let’s take a look at some basic syntax in Python.
Variables
In Python, a variable is a storage location that has a name and a value. We can assign a value to a variable by using the equal sign (=) like this:
x = 5
y = "Hello, World!"
In the example above, we have created two variables x
and y
and assigned them values 5
and "Hello, World!"
respectively. We can print the values of the variables by using the print()
function like this:
print(x)
print(y)
This will output:
5
Hello, World!
Data types
In Python, there are various data types such as integers, floats, strings, lists, tuples, and dictionaries. Let’s take a look at each of these data types.
Integers
Integers are whole numbers. We can assign integer values to variables like this:
x = 5
y = -10
Floats
Floats are numbers with a decimal point. We can assign float values to variables like this:
x = 3.14
y = -5.5
Strings
Strings are a sequence of characters. We can assign string values to variables like this:
x = "Hello, World!"
y = 'Python is awesome!'
Lists
Lists are used to store multiple values in a single variable. We can create a list by enclosing values in square brackets.
my_list = [1, 2, 3, 4, 5]
Tuples
Tuples are similar to lists, but their values cannot be changed once they are assigned. We can create a tuple by enclosing values in parentheses.
my_tuple = (1, 2, 3, 4, 5)
Dictionaries
Dictionaries are used to store key-value pairs. We can create a dictionary by enclosing key-value pairs in curly braces.
my_dict = {"name": "John", "age": 30, "city": "New York"}
Operators
Operators are special symbols that perform certain operations on variables or values. Let’s take a look at some operators in Python.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations.
Operator | Description |
---|---|
+ | Addition |
– | Subtraction |
* | Multiplication |
/ | Division |
% | Modulus (Remainder) |
** | Exponentiation |
// | Floor division |
Comparison operators
Comparison operators are used to compare two values.
Operator | Description |
---|---|
Equal | |
!= | Not equal |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
Logical operators
Logical operators are used to combine conditional statements.
Operator | Description |
---|---|
and | Returns True if both statements are true |
or | Returns True if one of the statements is true |
not | Reverse the result, returns False if the result is true |
Conditional statements
Conditional statements are used to make decisions in programming. There are three types of conditional statements in Python: if statements, if-else statements, and if-elif-else statements.
If statements
If statements are used to execute a block of code if a condition is true.
if condition:
# execute this code if condition is true
If-else statements
If-else statements are used to execute one block of code if a condition is true and another block of code if the condition is false.
if condition:
# execute this code if condition is true
else:
# execute this code if condition is false
If-elif-else statements
If-elif-else statements are used to test multiple conditions.
if condition1:
# execute this code if condition1 is true
elif condition2:
# execute this code if condition2 is true
else:
# execute this code if all conditions are false
Loops
Loops are used to execute a block of code repeatedly. There are two types of loops in Python: for loops and while loops.
For loops
For loops are used to iterate over a sequence (such as a list or tuple) or other iterable objects.
my_list = [1, 2, 3, 4, 5]
for num in my_list:
print(num)
While loops
While loops are used to execute a block of code as long as a condition is true.
i = 1
while i <= 10:
print(i)
i = i + 1
Functions
Functions are a block of reusable code that performs a specific task. We can create a function in Python using the def
keyword.
def my_function(parameter1, parameter2):
# code to be executed
return result
In the example above, my_function
is the name of the function. parameter1
and parameter2
are the parameters of the function. The code inside the function is executed when the function is called. The return
statement returns a result from the function.
Conclusion
In this tutorial, we have covered the basic concepts of Python 3 such as variables, data types, operators, conditional statements, loops, and functions. Python is a powerful and versatile programming language that can be used for a variety of tasks. We hope this tutorial has given you a good foundation to start coding in Python. Happy coding!