Professional Documents
Culture Documents
Lecture No 7
Lecture No 7
Introduction to
Python
April 7, 2024 1
Outline
• Introduction to Python (syntax, data types, if else, loops, operators, strings)
• Lists
• Tuples
• Sets
• Dictionaries
April 7, 2024 2
Introduction to Python
• Python is a high-level programming language known for its simplicity and readability. Its
syntax emphasizes code readability and allows programmers to express concepts in fewer
lines of code compared to other languages.
• There are no type declarations of variables, parameters, functions, or methods in source
code. This makes the code short and flexible.
• Python is an interpreted language, meaning that code is executed line by line. This makes
it easy to test code snippets
• Python is a versatile language used for a wide range of applications, including web
development, data analysis, artificial intelligence, machine learning, scientific computing,
and automation. Its extensive standard library and third-party packages make it suitable
for projects of any size and complexity.
April 7, 2024 3
Python Variables
April 7, 2024 4
Python Variables
April 7, 2024 5
Python Variables
April 7, 2024 6
Python Variables
Variable Names:
April 7, 2024 7
Python Variables
• Variable names with more than one word can be difficult to read.
• There are several techniques you can use to make them more readable:
April 7, 2024 8
Python Variables
• And you can assign the same value to multiple variables in one line
x = y = z = "Orange“
April 7, 2024 9
Python Variables
• Unpack a Collection:
• If we have a collection of values in a list, tuple etc. Python allows us to
extract the values into variables. This is called unpacking.
fruits = ["apple", "banana", "cherry"]
x, y, z = fruits
April 7, 2024 10
Python Variables
April 7, 2024 11
Python Variables
x=5
y = "John"
April 7, 2024 print(x, y) 12
Python Variables
• 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.
April 7, 2024 13
Python Variables
April 7, 2024 14
Python Variables
April 7, 2024 15
Python Variables
• To change the value of a global variable inside a function, refer to the variable by
using the global keyword
April 7, 2024 16
Indentation
• Python relies on indentation (whitespace at the beginning of a line) to define scope in
the code. Other programming languages often use curly-brackets for this purpose.
April 7, 2024 17
Python Conditions and If statements
• Python supports the usual logical conditions from mathematics:
April 7, 2024 18
Python Conditions and If statements
April 7, 2024 19
Python Conditions and If statements
April 7, 2024 20
Python Conditions and If statements
April 7, 2024 21
Python Conditions and If statements
April 7, 2024 22
Python Conditions and If statements
April 7, 2024 23
Python Conditions and If statements
April 7, 2024 24
Python Conditions and If statements
April 7, 2024 25
Python Conditions and If statements
April 7, 2024 26
Python Conditions and If statements
April 7, 2024 27
Python Conditions and If statements
April 7, 2024 28
Python Loops
Python has two primitive loop commands:
• while loops
• for loops
April 7, 2024 29
Python Loops
April 7, 2024 30
Python Loops
April 7, 2024 31
Python Loops
April 7, 2024 32
Python Operators
Python divides the operators in the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Bitwise operators
April 7, 2024 33
Python Strings
April 7, 2024 34
Python Strings
April 7, 2024 35
Python Strings
April 7, 2024 36
Python Strings
April 7, 2024 37
Python Data Type:
In Python, the data type is set when you assign a value to a variable.
April 7, 2024 38
Python Data Type:
In Python, the data type is set when you assign a value to a variable.
April 7, 2024 39
Python Strings
April 7, 2024 40
Python Collections (Arrays)
There are four collection data types in the Python programming language:
List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.
April 7, 2024 41
Python List
April 7, 2024 42
Python List
April 7, 2024 44
Python List (Change List Items)
April 7, 2024 45
Python List (Add List Items)
April 7, 2024 46
Python List (Loop through a List)
April 7, 2024 47
Python Tuple
April 7, 2024 48
Python Tuple
• Access tuple
• Update tuple
• Unpack
• Join tuple
• Count
• Index
April 7, 2024 49