Python Introduction
By
Dr Bijoy Kumar Mandal
What is Python?
• Python is a popular programming language. Python can be used on a server
to create web applications. 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 similar to 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-orientated way or
a functional way.
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.
Installation Python
• 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):
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:
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 Quick start
• 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:
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.py
print("Hello, World!")
• Simple as that. Save your file. Open your command line, navigate to
the directory where you saved your file, and run:
• The output should read:
Congratulations, you have written and executed your first Python
program.
Python Variables
Creating Variables
• Variables are containers for storing data values.
• Unlike other programming languages, 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)
• String variables can be declared either by using single or double quotes
• Example
x = "John"
# is the same as
x = 'John‘
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)
• Example
• #Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
#Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Assign Value to Multiple Variables
• Python allows you to assign values to multiple variables in one line
• Example
• x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
• And you can assign the same value to multiple variables in one line
• Example
• x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables
• The Python print statement is often used to output variables
• To combine both text and a variable, Python uses the + character
• Example
x = "awesome"
print("Python is " + x)
• You can also use the + character to add a variable to another variable
• Example
x = "Python is "
y = "awesome"
z= x+y
print(z)
• For numbers, the + character works as a mathematical operator
• Example
x=5
y = 10
print(x + y)
• If you try to combine a string and a number, Python will give you an
error
• Example
x=5
y = "John"
print(x + y)
Global Variables
• Variables that are created outside of a function (as in all of the examples above)
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()
• 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)
• 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
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
To verify the type of any object in Python, use the type() function:
Example
print(type(x))
print(type(y))
print(type(z))
• int
int, or integer, is a whole number, positive or negative, without decimals, of unlimited length.
• Example
Integers:
x=1
y = 35656222554887711
z = -3255522
print(type(x))
print(type(y))
print(type(z))
• Float
Float, or "floating point number" is a number, positive or negative, containing one or more decimals.
Example
Floats:
x = 1.10
y = 1.0
z = -35.59
print(type(x))
print(type(y))
print(type(z))
• Float can also be scientific numbers with an "e" to indicate the power of 10.
• Example
Floats:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
• Complex
Complex numbers are written with a "j" as the imaginary part:
• Example
Complex:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
Type Conversion
• You can convert from one type to another with the int(), float(), and complex() methods:
• Example
Convert from one type to another:
x = 1 # int
y = 2.8 # float
z = 1j # complex
#convert from int to float:
a = float(x)
#convert from float to int:
b = int(y)
#convert from int to complex:
c = complex(x)
print(a)
print(b)
print(c)
print(type(a))
print(type(b))
print(type(c))
Note: You cannot convert complex numbers into another number type.
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 between 1 and
9:
• import random
• print(random.randrange(1, 10))
Python Casting
Specify a Variable Type
• There may be times when you want to specify a type on to a variable. This can be done
with casting. Python is an object-orientated language, and as such it uses classes to define
data types, including its primitive types.
• Casting in python is therefore done using constructor functions:
• int() - constructs an integer number from an integer literal, a float literal (by rounding
down to the previous whole number), or a string literal (providing the string represents a
whole number)
• float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
• str() - constructs a string from a wide variety of data types, including strings, integer
literals and float literals
• Example
• Integers:
x = int(1) # x will be 1
y = int(2.8) # y will be 2
z = int("3") # z will be 3
• Example
• Floats:
x = float(1) # x will be 1.0
y = float(2.8) # y will be 2.8
z = float("3") # z will be 3.0
w = float("4.2") # w will be 4.2
• Example
• Strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
Python Comments
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
• Comments does not have to be text to explain the code, it can also be used to
prevent Python from executing code:
• Example
#print("Hello, World!")
print("Cheers, Mate!")
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
Thank You