You are on page 1of 36

Chapter 9

Introduction to Python
Declaration
 These slides are made for UIT, BU students only. I am not
holding any copy write of it as I had collected these study
materials from different books and websites etc. I have not
mentioned those to avoid complexity.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.2


Topics
 Features
 Python compiler
 Variable and data types
 Operators
 Looping
 Conditional and control statement
 String manipulation
 List
 Tuple
 Dictionary

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.3


Python Features
 Python is a dynamic, high-level, free open source, and
interpreted programming language. It supports object-oriented
programming as well as procedural-oriented programming.
In Python, we don’t need to declare the type of variable because
it is a dynamically typed language.
1. Free and Open Source: Python language is freely available at
the official website and you can download it. Open-source
means that source code is also available to the public. So you
can download it, use it as well as share it.
2. Easy to code: Python is a high-level programming language.
Python is very easy to learn the language as compared to other
languages like C, C#, Javascript, Java, etc. It is very easy to
code in the Python language and anybody can learn Python
basics in a few hours or days. It is also a developer-friendly
language.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.4


Python Features
3. Easy to Read: As you will see, learning Python is quite simple.
As was already established, Python’s syntax is really
straightforward. The code block is defined by the indentations
rather than by semicolons or brackets.
4. Object-Oriented Language: One of the key features of Python
is Object-Oriented programming. Python supports object-
oriented language and concepts of classes, object
encapsulation, etc.
5. GUI Programming Support: Graphical User interfaces can be
made using a module such as PyQt5, PyQt4, wxPython, or Tk
in python. PyQt5 is the most popular option for creating
graphical apps with Python.
6. High-Level Language: Python is a high-level language. When
we write programs in Python, we do not need to remember the
system architecture, nor do we need to manage the memory.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.5


Python Features
7. Extensible feature: Python is an Extensible language. We
can write some Python code into C or C++ language and also
we can compile that code in C/C++ language.
8. Easy to Debug: Excellent information for mistake tracing. You
will be able to quickly identify and correct the majority of your
program’s issues once you understand how
to interpret Python’s error traces. Simply by glancing at the
code, you can determine what it is designed to perform.
9. Python is a Portable language: Python language is also a
portable language. For example, if we have Python code for
windows with certain architecture and if we want to run this
code on other platforms such as Linux, Unix, and Mac with
another architecture then we do not need to change it, we can
run this code on any platform.
10. Python is an Integrated language: Python is also an
Integrated language because we can easily integrate Python
with other languages like C, C++, etc.
8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.6
Python Features
11. Interpreted Language: Python is an Interpreted Language
because Python code is executed line by line at a time.
12. Large Standard Library: Python has a large standard
library that provides a rich set of modules and functions so you
do not have to write your own code for every single thing.
13. Dynamically Typed Language: Python is a dynamically-typed
language. That means the type (for example- int, double, long,
etc.) for a variable is decided at run time not in advance
because of this feature we don’t need to specify the type of
variable.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.7


Python Features
14. Frontend and backend development: With a new project py
script, you can run and write Python codes in HTML with the
help of some simple tags <py-script>, <py-env>, etc. This will
help you do frontend development work in Python like
javascript. Backend is the strong forte of Python it’s extensively
used for this work cause of its frameworks
like Django and Flask.
15. Allocating Memory Dynamically: In Python, the variable data
type does not need to be specified. The memory is
automatically allocated to a variable at runtime when it is given
a value. Developers do not need to write int y = 18 if the integer
value 15 is set to y. You may just type y=18.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.8


Understanding the Execution of Python
Program
 Let’s consider the below example.
a = 10
b = 10
print("Sum ", (a+b))
Output:
Sum 20
 Suppose the above python program is saved as first.py. Here
first is the name and .py is the extension. The execution of the
Python program involves 2 Steps:
 Compilation
 Interpreter

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.9


Understanding the Execution of Python
Program
 Compilation: The program is converted into byte code. Byte
code is a fixed set of instructions that represent arithmetic,
comparison, memory operations, etc. It can run on any operating
system and hardware i.e. platform independent. The byte code
instructions are created in the .pyc file. The .pyc file is not
explicitly created as Python handles it internally.
 Interpreter: The next step involves converting the byte code
(.pyc file) into machine code. This step is necessary as the
computer can understand only machine code (binary code).
Python Virtual Machine (PVM) first understands the operating
system and processor in the computer and then converts it into
machine code. Further, these machine code instructions are
executed by processor and the results are displayed.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.10


Understanding the Execution of Python
Program

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.11


Understanding the Execution of Python
Program
 However, the interpreter inside the PVM translates the program
line by line thereby consuming a lot of time. To overcome this, a
compiler known as Just In Time (JIT) is added to PVM. JIT
compiler improves the execution speed of the Python program.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.12


What Are Variables In Python?
 Variables and data types in python as the name suggests are the
values that vary. In a programming language, a variable is a
memory location where you store a value. The value that you
have stored may change in the future according to the
specifications.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.13


What Are Variables In Python?
 A Variable in python is created as soon as a value is assigned to
it. It does not need any additional commands to declare a
variable in python.

8/9/2023 11:48 PM Dr. Dipankar Dutta, UIT, BU 1.14


Variable Definition & Declaration
 Python has no additional commands to declare a variable. As
soon as the value is assigned to it, the variable is declared.
x = 10
# variable is declared as the value 10 is assigned to it.
 There are a certain rules that we have to keep in mind while
declaring a variable:
 The variable name cannot start with a number. It can only start with
a character or an underscore.
 Variables in python are case sensitive.
 They can only contain alpha-numeric characters and underscores.
 No special characters are allowed.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.15


Data Types In Python
 There are several data types in python. Lets take a look at the
data types in python.
 Every value that we declare in python has a data type. Data
types are classes and variables are the instances of these
classes.
 According to the properties they possess, there are mainly six
data types in python. Although there is one more data type range
which is often used while working with loops in python.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.16


type function
 The type() function is used to determine the type of a given
object. It returns the class type of the object. Here's the general
syntax:
 type(object)
 Example
x=5
y = "Hello, world!“
z = [1, 2, 3]
print(type(x)) # Output: <class 'int'>
print(type(y)) # Output: <class 'str'>
print(type(z)) # Output: <class 'list'>

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.17


Numerical Data Types
 Numerical data type holds numerical value. In numerical data
there are 4 sub types as well. Following are the sub-types of
numerical data type:
 Integers
 Float
 Complex Numbers
 Boolean
 Integers are used to represent whole number values.
x = 100
y = 124
To check the type of any variable data type, we can use
the type() function.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.18


Numerical Data Types
 Float data type is used to represent decimal point values.
x = 10.25
y = 12.30
 Complex numbers are used to represent imaginary values.
Imaginary values are denoted with ‘j’ at the end of the number.
x = 10 + 5j
 Boolean is used for categorical output, since the output of
boolean is either true or false.
num = 5 > 4
# num is the boolean variable
type(num)
# the output will be bool
print(num)
#this will print true.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.19


Strings
 Strings in python are used to represent unicode character
values. Python does not have a character data type, a single
character is also considered as a string.
 We denote or declare the string values inside single quotes or
double quotes. To access the values in a string, we use the
indexes and square brackets.
name = 'edureka'
name[2]
# this will give you the output as 'u'
 Strings are immutable in nature, which means you cannot
change a string once replaced.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.20


Command line input for strings
x = input()
print( 'hello' , x)

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.21


Operations using strings
name = 'edureka'
name.upper()
# this will make the letters to uppercase
name.lower()
# this will make the letters to lowercase
name.replace('e') = 'E'
# this will replace the letter 'e' with 'E'
name[1: 4]
# this will return the strings starting at index 1 until the index 4.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.22


Lists
 List is one of the four collection data type that we have in python.
When we are choosing a collection type, it is important to
understand the functionality and limitations of the collection.
Tuple, set and dictionary are the other collection data type is
python.
 A list is ordered and changeable, unlike strings. We can add
duplicate values as well. To declare a list we use the square
brackets.
mylist = [10,20,30,40,20,30, 'edureka']
 Lists can store any data type as items. Be it numbers, strings or
any other data type as well.
 Accessing values from a list
 We use indexes to access values from a string.
mylist[2:6]
# this will get the values from index 2 until index 6.
8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.23
Lists
 Adding/Replacing values in a list
mylist[6] = 'python'

# this will replace the value at the index 6.

mylist.append('edureka')

# this will add the value at the end of the list.

mylist.insert(5, 'data science')

# this will add the value at the index 5.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.24


Lists
clear() removes all the elements from the list
copy() returns a copy of the list
add the elements of the list to the end of
extend()
the current list
returns the number of elements of the
count()
specified value
index() returns the index of the element
removes the element from the specified
pop()
position

remove() removes the item with the specified value

sort() sorts the list

reverse() returns the reversed list


8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.25
Tuples
 Tuple is a collection which is unchangeable or immutable. It is
ordered and the values can be accessed using the index values.
A tuple can have duplicate values as well. To declare a tuple we
use the round brackets.

mytuple = (10,10,20,30,40,50)
#to count the number of elements
mytuple.count(10)
# the output will be 2
# to find the index
mytuple.index(50)
# the output will be 5. since the index number at 50 is 5.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.26


Sets
 A set is a collection which is unordered, it does not have any
indexes as well. To declare a set in python we use the curly
brackets.
myset = {10, 20 , 30 ,40, 50, 50}
 A set does not have any duplicate values, even though it will not
show any errors while declaring the set, the output will only have
the distinct values.
 To access the values in a set we can either loop through the set,
or use a membership operator to find a particular value.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.27


Sets
for x in myset:
print(x)
#this will get all the values.
20 in myset
#this will return true if the value is in the set.
#to add a value in a set
myset.add('edureka')
#to add multiple values in a list
myset.update([ 10, 20, 30, 40, 50])
#to remove an item from a set
myset.remove('edureka')
#we can use the discard or pop method to remove an item from a set
as well.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.28


Sets
myset = {10, 20, 30}
myset1 = {10,30,50}
myset.issubset(myset1)
# this will return false
myset.union(myset1)
# this will return a set with the union of the two sets.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.29


Sets

clear() clears the items from a set

copy() returns the copy of the set

returns a set with the difference


difference()
of the two sets
returns if the sets have
isdisjoint()
intersection

issubset() returns if the set is a subset

returns a set with the symmetric


symmetricdifference()
difference
update the sets with union of
update()
the set

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.30


Dictionary
 A dictionary is just like any other collection array in python. But
they have key value pairs. A dictionary is unordered and
changeable. We use the keys to access the items from a
dictionary. To declare a dictionary, we use the curly brackets.

mydictionary = { 'python': 'data science', 'machine learning' : 'tensorflow'


, 'artificial intelligence': 'keras'}
mydictionary['machine learning']
# this will give the output as 'tensorflow'
mydictionary.get('python')
# this serves the same purpose to access the value.
 Since we are using the keys to access the items, they cannot be
duplicate. The values can have duplicate items.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.31


Dictionary
#adding a new value

mydictionary['analysis'] = 'matplotlib'

#replacing a value

mydictionary['analysis'] = 'pandas'

#deleting a value

mydictionary.pop('analysis')

#remove() , del also serves the same purpose for deleting a value.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.32


Dictionary
copy() returns a copy of the dictionary
clear() clears the dictionary
items() returns a list containing tuple of key value pairs
keys() returns a list containing all the keys
update() updates the dictionary with all the key-value pairs
values() returns a list of all the values in a dictionary
setdefault() returns the value of a specified key

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.33


Range
 Range is a data type which is mainly used when we are using a
loop. Lets take an example to understand this.
for x in range(10):
print(x)
# this will print the numbers from 0-10. Range will have the numbers
from 0-10

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.34


Type Casting
 Type casting basically is the process of changing one data type
into another. We have constructors for each of the data types in
python.
 list()
 set()
 tuple()
 dict()
 str()
 int()
 float()

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.35


Type Casting
 We can simply use these constructors to use the specified data
type or we can change a data type to another using these
constructors. Lets understand this with an example.
a = [10, 20 ,30, 40]

#to change this list into a tuple i can simply write

tuple(a)

#now the list will change to a tuple.

8/9/2023 11:49 PM Dr. Dipankar Dutta, UIT, BU 1.36

You might also like