Python Introduction
Python is a popular programming language.
It was created by Guido van Rossum, and released in 1991.
It is used for:
➢web development (server-side),
➢software development,
➢mathematics,
➢system scripting.
What can Python do?
➢Python can be used on a server to create web applications.
➢Python can be used alongside software to create workflows.
➢Python can connect to database systems. It can also read and modify
files.
➢Python can be used to handle big data and perform complex
mathematics.
➢Python can be used for rapid prototyping, or for production-ready
software development.
Why Python?
➢Python works on different platforms (Windows, Mac, Linux,
Raspberry Pi, etc.).
➢Python has a simple syntax like the English language.
➢Python has syntax that allows developers to write programs with
fewer lines than some other programming languages.
➢Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
➢Python can be treated in a procedural way, an object-oriented way or a
functional way.
Good to know
➢The most recent major version of Python is Python 3, which we shall
be using in this tutorial.
➢In this tutorial Python will be written in a text editor. It is possible to
write Python in an Integrated Development Environment, such as
Thonny, Pycharm, Netbeans or Eclipse which are particularly useful
when managing larger collections of Python files.
Python Syntax compared to other
programming languages
➢Python was designed for readability and has some similarities to the
English language with influence from mathematics.
➢Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
➢Python relies on indentation, using whitespace, to define scope; such as
the scope of loops, functions and classes. Other programming languages
often use curly-brackets for this purpose.
Example
print("Hello, World!")
Execute Python Syntax
Python syntax can be executed by writing directly in the Command
Line:
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
if 5 > 2:
print("Five is greater than two!").
Python will give you an error if you skip the indentation:
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 must be at least one.
if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
You must use the same number of spaces in the same block of code;
otherwise, Python will give you an 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:
Python has no command for declaring a variable.
x=5
y = "Hello, World!"
print(x)
print(y)
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:
#This is a comment.
print("Hello, World!")
Python Statements
➢A computer program is a list of "instructions" to be "executed" by a
computer.
➢In a Python language, these programming instructions are
called statements.
➢The following statement prints the text "Python is fun!" to the screen:
print("Python is fun!")
Python Statements
➢In Python, a statement usually ends when the line ends. You do not need to use a
semicolon (;) like in many other programming languages (for example, Java or C).
➢Most Python programs contain many statements.
➢The statements are executed one by one, in the same order as they are written:
print("Hello World!")
print("Have a good day.")
print("Learning Python is fun!")
Semicolons (Optional, Rarely Used)
Semicolons are optional in Python. You can write multiple statements
on one line by separating them with ; but this is rarely used because it
makes it hard to read:
print("Hello"); print("How are you?"); print("Bye bye!")
Semicolons (Optional, Rarely Used)
However, if you put two statements on the same line without a separator
(newline or ;), Python will give an error:
print("Python is fun!") print("Really!")
Best practice: Put each statement on its own line so your code is easy
to understand.
Python Syntax Code Challenge
Challenge: Statements
➢Test your understanding of Python statements by completing a small
coding challenge.
Instructions
Inside the editor, complete the following steps:
• Write a statement that prints "Hello World!"
• Write a statement that prints "Have a good day."
• Write a statement that prints "Learning Python is fun!"
Double Quotes
➢Text in Python must be inside quotes. You can use either " double
quotes or ' single quotes:
print("This will work!")
print('This will also work!’)
Double Quotes
If you forget to put the text inside quotes, Python will give an error:
print(This will cause an error)
Print Without a New Line
➢By default, the print() function ends with a new line.
➢If you want to print multiple words on the same line, you can use
the end parameter:
print("Hello World!", end=" ")
print("I will print on the same line.")
Note that we add a space after end=" " for better readability.
Python Output Numbers
Print Numbers
➢You can also use the print() function to display numbers:
➢However, unlike text, we don't put numbers inside double quotes:
print(3)
print(358)
print(50000)
Python Output Numbers
You can also do math inside the print() function:
print(3 + 3)
print(2 * 5)
Mix Text and Numbers
You can combine text and numbers in one output by separating them
with a comma:
print("I am", 35, "years old.")
Python Output Code Challenge
Challenge: Output / Print
➢Test your understanding of Python output by completing a small
coding challenge.
Instructions
➢Inside the editor, complete the following steps:
➢Print the text "I am" and the number 25 in one print statement
Python Variables
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.
x=5
y = "John"
print(x)
print(y)
Python Variables
Variables do not need to be declared with any type and can even change
type after they have been set.
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.
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.
x=5
y = "John"
print(type(x))
print(type(y))
Single or Double Quotes?
String variables can be declared either by using single or double quotes:
x = "John"
print(x)
#double quotes are the same as single quotes:
x = 'John'
print(x)
Case-Sensitive
Variable names are case-sensitive.
a=4
A = "Sally"
#A will not overwrite a
print(a)
print(A)
Python - Variable Names
Variable Names
• A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
Rules for Python variables:
• A variable name must start with a letter or the underscore
character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three
different variables)
• A variable name cannot be any of the Python keywords.
Example: Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
print(myvar)
print(my_var)
print(_my_var)
print(myVar)
print(MYVAR)
print(myvar2)
Example: Illegal variable names
2myvar = "John"
my-var = "John"
my var = "John"
#This example will produce an error in the result
Example: Illegal variable names
Multi Words Variable Names
• Variable names with more than one word can be difficult to read.
• There are several techniques you can use to make them more readable:
Camel Case
• Each word, except the first, starts with a capital letter:
myVariableName = "John"
Pascal Case
• Each word starts with a capital letter:
MyVariableName = "John"
Example: Illegal variable names
Snake Case
• Each word is separated by an underscore character:
my_variable_name = "John"
Python Variables - Assign Multiple Values
Many Values to Multiple Variables
• Python allows you to assign values to multiple variables in one
line:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Note: Make sure the number of variables matches the number of values, or else you
will get an error.
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
x = y = z = "Orange"
print(x)
print(y)
print(z)
Unpack a Collection
If you have a collection of values in a list, tuple etc. Python allows
you to extract the values into variables. This is called unpacking.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
print(x)
print(y)
print(z)
Python - Output Variables
Output Variables
• The print() function is often used to output variables.
x = "Python is awesome"
print(x)
Python - Output Variables
In the print() function, you output multiple variables, separated by a
comma:
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
Python - Output Variables
You can also use the + operator to output multiple variables:
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Notice the space character after "Python " and "is ", without them the result
would be "Pythonisawesome".
Python - Global Variables
Global Variables
• Variables that are created outside of a function (as in all of the
examples in the previous pages) are known as global variables.
• Global variables can be used by everyone, both inside of functions and
outside.
Example
• Create a variable outside of a function, and use it inside the function
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Python - Global Variables
If you create a variable with the same name inside a function, this variable will
be local and can only be used inside the function. The global variable with the
same name will remain as it was, global and with the original value.
Example
• Create a variable inside a function, with the same name as the global variable
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
The global Keyword
• Normally, when you create a variable inside a function, that variable is
local, and can only be used inside that function.
• To create a global variable inside a function, you can use
the global keyword.
Example
• If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
The global Keyword
• Also, use the global keyword if you want to change a global variable inside a
function.
Example
• To change the value of a global variable inside a function, refer to the variable by
using the global keyword:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
Python Numbers
Python Numbers
• There are three numeric types in Python:
➢int
➢float
➢complex
• Variables of numeric types are created when you assign a value to
them:
Example
x = 1 # int
y = 2.8 # float
z = 1j # complex
Python Numbers
To verify the type of any object in Python, use the type() function:
x=1
y = 2.8
z = 1j
print(type(x))
print(type(y))
print(type(z))
Python Numbers
Int, or integer, is a whole number, positive or negative, without
decimals, of unlimited length.
Example
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
Python Numbers
Float, or "floating point number" is a number, positive or negative,
containing one or more decimals.
Example
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
Python Numbers
Float can also be scientific numbers with an "e" to indicate the power of
10.
Example
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Python Numbers
Complex numbers are written with a "j" as the imaginary part:
Example
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Python Numbers
Type Conversion
• You can convert from one type to #convert from int to complex:
another with the int(), float(), c = complex(x)
and complex() methods:
print(a)
Example print(b)
• Convert from one type to another: print(c)
x = 1 # int print(type(a))
y = 2.8 # float print(type(b))
z = 1j # complex print(type(c))
#convert from int to float:
a = float(x)
Note: You cannot convert complex numbers into
another number type.
#convert from float to int:
b = int(y)
Python Numbers
Random Number
• Python does not have a random() function to make a random number,
but Python has a built-in module called random that can be used to
make random numbers:
Example
• Import the random module, and display a random number from 1 to 9:
import random
print([Link](1, 10))