
Learn Python
Python is a popular programming language.
Python can be used on a server to create web applications.
Learning by Examples
With our “Try it Yourself” editor, you can edit Python code and view the result.
Example
print(“Hello, World!”)
What exactly is Python?
Python is one of the most widely used programming languages. Guido van Rossum created it, and it was released in 1991.
It is employed for the following purposes:
growth of the internet (server-side),
development of software
mathematics,
Scripting for the operating system
What does Python have to offer?
Python can be used to construct web applications on a server.
Python may be used to develop workflows in conjunction with other software.
Python has the ability to connect to database systems. It also has the ability to read and change files.
Python may be used to work with large amounts of data and execute complex calculations.
Python may be used for rapid prototyping as well as creation of production-ready software.
What makes Python so special?
Python may be used on a variety of platforms (Windows, Mac, Linux, Raspberry Pi, etc).
Python has a basic grammar that is similar to that of English.
Python offers a syntax that enables programmers to write code.
Python Install
Many PCs and Macs will have python already installed.
To check if you have python installed on a Windows PC, search in the start bar for Python or run the following on the Command Line (cmd.exe):C:\Users\Your Name>python –version
To check if you have python installed on a Linux or Mac, then on linux open the command line or on Mac open the Terminal and type:python –version
If you find that you do not have Python installed on your computer, then you can download it for free from the following website: https://www.python.org/
Python Quickstart
Python is an interpreted programming language, this means that as a developer you write Python (.py) files in a text editor and then put those files into the python interpreter to be executed.
The way to run a python file is like this on the command line:C:\Users\Your Name>python helloworld.py
Where “helloworld.py” is the name of your python file.
Let’s write our first Python file, called helloworld.py, which can be done in any text editor.
helloworld.pyprint(“Hello, World!”)
Execute Python Syntax
As we learned in the previous page, Python syntax can be executed by writing directly in the Command Line:>>> print(“Hello, World!”)
Hello, World!
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important.
Python uses indentation to indicate a block of code.
Example
if 5 > 2:
print(“Five is greater than two!”)
Python will give you an error if you skip the indentation:
Example
Syntax Error:if 5 > 2:
print(“Five is greater than two!”)
The number of spaces is up to you as a programmer, the most common use is four, but it has to be at least one.
Example
if 5 > 2:
print(“Five is greater than two!”)
if 5 > 2:
print(“Five is greater than two!”)
You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:
Example
Syntax Error:if 5 > 2:
print(“Five is greater than two!”)
print(“Five is greater than two!”)
Python Variables
In Python, variables are created when you assign a value to it:
Example
Variables in Python:x = 5
y = “Hello, World!”
Python has no command for declaring a variable.
You will learn more about variables in the Python Variables chapter.
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
Example
Comments in Python:#This is a comment.
print(“Hello, World!”)
Test Yourself With Exercises
Exercise:
Insert the missing part of the code below to output “Hello World”.
("Hello World")
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code.
Creating a Comment
Comments starts with a #
, and Python will ignore them:
Example
#This is a comment
print(“Hello, World!”)
Comments can be placed at the end of a line, and Python will ignore the rest of the line:
Example
print(“Hello, World!”) #This is a comment
A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code:
Example
#print(“Hello, World!”)
print(“Cheers, Mate!”)
ADVERTISEMENT
Multi Line Comments
Python does not really have a syntax for multi line comments.
To add a multiline comment you could insert a #
for each line:
Example
#This is a comment
#written in
#more than just one line
print(“Hello, World!”)
Or, not quite as intended, you can use a multiline string.
Since Python will ignore string literals that are not assigned to a variable, you can add a multiline string (triple quotes) in your code, and place your comment inside it:
Example
“””
This is a comment
written in
more than just one line
“””
print(“Hello, World!”)
As long as the string is not assigned to a variable, Python will read the code, but then ignore it, and you have made a multiline comment.
Test Yourself With Exercises
Exercise:
Comments in Python are written with a special character, which one?
Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Example
x = 5
y = “John”
print(x)
print(y)
Variables do not need to be declared with any particular type, and can even change type after they have been set.
Example
x = 4 # x is of type int
x = “Sally” # x is now of type str
print(x)
Casting
If you want to specify the data type of a variable, this can be done with casting.
Example
x = str(3) # x will be ‘3’
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Get the Type
You can get the data type of a variable with the type()
function.
Example
x = 5
y = “John”
print(type(x))
print(type(y)) You will learn more about data types and casting later in this tutorial.
Single or Double Quotes?
String variables can be declared either by using single or double quotes:
Example
x = “John”
# is the same as
x = ‘John’
Case-Sensitive
Variable names are case-sensitive.
Example
This will create two variables:a = 4
A = “Sally”
#A will not overwrite a
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Text Type: | str |
Numeric Types: | int , float , complex |
Sequence Types: | list , tuple , range |
Mapping Type: | dict |
Set Types: | set , frozenset |
Boolean Type: | bool |
Binary Types: | bytes , bytearray , memoryview |
Getting the Data Type
You can get the data type of any object by using the type()
function:
Example
Print the data type of the variable x:x = 5
print(type(x))
Setting the Data Type
In Python, the data type is set when you assign a value to a variable:
Example | Data Type | Try it |
---|---|---|
x = “Hello World” | str | |
x = 20 | int | |
x = 20.5 | float | |
x = 1j | complex | |
x = [“apple”, “banana”, “cherry”] | list | |
x = (“apple”, “banana”, “cherry”) | tuple | |
x = range(6) | range | |
x = {“name” : “John”, “age” : 36} | dict | |
x = {“apple”, “banana”, “cherry”} | set | |
x = frozenset({“apple”, “banana”, “cherry”}) | frozenset | |
x = True | bool | |
x = b”Hello” | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview |
If you want to specify the data type, you can use the following constructor functions:
Example | Data Type | Try it |
---|---|---|
x = str(“Hello World”) | str | |
x = int(20) | int | |
x = float(20.5) | float | |
x = complex(1j) | complex | |
x = list((“apple”, “banana”, “cherry”)) | list | |
x = tuple((“apple”, “banana”, “cherry”)) | tuple | |
x = range(6) | range | |
x = dict(name=”John”, age=36) | dict | |
x = set((“apple”, “banana”, “cherry”)) | set | |
x = frozenset((“apple”, “banana”, “cherry”)) | frozenset | |
x = bool(5) | bool | |
x = bytes(5) | bytes | |
x = bytearray(5) | bytearray | |
x = memoryview(bytes(5)) | memoryview |
Test Yourself With Exercises
Exercise:
The following code example would print the data type of x, what data type would that be?
x = 5 print(type(x))