0% found this document useful (0 votes)
9 views69 pages

Lecture No 7

The document introduces Python by describing its syntax, data types, variables, conditions, loops, operators, and strings. It then covers Python lists, tuples, sets, and dictionaries as well as built-in data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views69 pages

Lecture No 7

The document introduces Python by describing its syntax, data types, variables, conditions, loops, operators, and strings. It then covers Python lists, tuples, sets, and dictionaries as well as built-in data types.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 69

Lecture No 7

Introduction to
Python

May 14, 2024 1


Outline
• Introduction to Python (syntax, data types, if else, loops, operators, strings)
• Lists
• Tuples
• Sets
• Dictionaries

May 14, 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.

May 14, 2024 3


Python Variables

• Variables are containers for storing data values.


• Python has no command for declaring a variable.
• A variable is created the moment you first assign a
value to it.

• Variables do not need to be declared with any


particular type, and can even change type after they
have been set.

May 14, 2024 4


Python Variables

• If we want to specify the data type of a variable,


this can be done with casting.
• String variables can be declared either by using
single or double quotes:

• We can get the data type of a variable with the


type() function.

May 14, 2024 5


Python Variables

• String variables can be declared


either by using single or double
quotes.

May 14, 2024 6


Python Variables

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 keyword.

May 14, 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:

May 14, 2024 8


Python Variables

• Python allows us to assign values to multiple variables in one line


x, y, z = "Orange", "Banana", "Cherry“

• And you can assign the same value to multiple variables in one line
x = y = z = "Orange“

May 14, 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

May 14, 2024 10


Python Variables

• The Python print() function is often used to output variables.


x = "Python is awesome"
print(x)
• In the print() function, we can output multiple variables,
separated by a comma:
x = "Python“, y = "is“, z = "awesome"
print(x, y, z)
• You can also use the + operator to output multiple variables:
x = "Python “, y = "is “, z = "awesome"
print(x + y + z)

May 14, 2024 11


Python Variables

• For numbers, the + character works as a mathematical operator:


x=5
y = 10
print(x + y)
• When we try to combine a string number with + operator, Python with give an error.
x=5
y = "John"
print(x + y)
• The best way to output multiple variable in print() function is to separate them with
commas, which even support different data types.

x=5
y = "John"
May 14, 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.

May 14, 2024 13


Python Variables

• If we 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.

May 14, 2024 14


Python Variables

• 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.

May 14, 2024 15


Python Variables

• To change the value of a global variable inside a function, refer to the variable by
using the global keyword

May 14, 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.

May 14, 2024 17


Python Conditions and If statements
• Python supports the usual logical conditions from mathematics:

These conditions can be used in several ways, most commonly in "if


statements" and loops.

May 14, 2024 18


Python Conditions and If statements

May 14, 2024 19


Python Conditions and If statements

May 14, 2024 20


Python Conditions and If statements

May 14, 2024 21


Python Conditions and If statements

May 14, 2024 22


Python Conditions and If statements

May 14, 2024 23


Python Conditions and If statements

May 14, 2024 24


Python Conditions and If statements

May 14, 2024 25


Python Conditions and If statements

May 14, 2024 26


Python Conditions and If statements

May 14, 2024 27


Python Conditions and If statements

May 14, 2024 28


Python Loops
Python has two primitive loop commands:
• while loops
• for loops

May 14, 2024 29


Python Loops

May 14, 2024 30


Python Loops

May 14, 2024 31


Python Loops

May 14, 2024 32


Python Operators
Python divides the operators in the following groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
•Bitwise operators

May 14, 2024 33


Python Strings

May 14, 2024 34


Python Strings

May 14, 2024 35


Python Strings

May 14, 2024 36


Python Strings

Other String functions:


-> Slicing String
-> Modify String
-> Concatenating String
-> so on . . .

May 14, 2024 37


Python Data Type:
In Python, the data type is set when you assign a value to a variable.

May 14, 2024 38


Python Data Type:
In Python, the data type is set when you assign a value to a variable.

May 14, 2024 39


Python Strings

• 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:

May 14, 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.

May 14, 2024 41


Python List

• Lists are used to store multiple items in a single variable.


• thislist = ["apple", "banana", "cherry"]
• List items are ordered, changeable, and allow duplicate values.
• List items are indexed, the first item has index [0], the second item has
index [1] etc.
• If we add new items to a list, the new items will be placed at the end of
the list.

May 14, 2024 42


Python List

• To determine how many items a list has, use


the len() function

• List items can be of any data type:

• A list can contain different data types:

• From Python's perspective, lists are defined


as objects with the data type 'list':
May 14, 2024 43
Python List (Access List Items)

May 14, 2024 44


Python List (Change List Items)

May 14, 2024 45


Python List (Add List Items)

May 14, 2024 46


Python List (Loop through a List)

May 14, 2024 47


Python Tuple

• Tuples are used to store multiple items in a single variable.


• A tuple is a collection which is ordered, unchangeable and allow
duplicates.
• Tuples are written with round brackets.

May 14, 2024 48


Python Tuple

• Access tuple
• Update tuple
• Unpack
• Join tuple
• Count
• Index

May 14, 2024 49


Python Sets

• Sets are used to store multiple items in a single variable.

• Set is one of 4 built-in data types in Python used to store collections of


data, the other 3 are List, Tuple, and Dictionary, all with different
qualities and usage.

• A set is a collection which is unordered, unchangeable, and unindexed.

• Sets are written with curly brackets.


myset = {"apple", "banana", "cherry"}

May 14, 2024 50


Python Sets

May 14, 2024 51


Python Sets

May 14, 2024 52


Python Sets

May 14, 2024 53


Python Sets

May 14, 2024 54


Python Sets

May 14, 2024 55


Python Sets (Access Set Items)

May 14, 2024 56


Python Sets (Add Set Items)

May 14, 2024 57


Python Sets (Remove Set Items)

May 14, 2024 58


Python Sets (Remove Set Items)

May 14, 2024 59


Python Sets (Join Sets)

May 14, 2024 60


Python Sets (Join Sets)

May 14, 2024 61


Python Sets (Join Sets)

May 14, 2024 62


Python Dictionaries

• Dictionaries are used to store data values in key:value pairs.

• A dictionary is a collection which is ordered*, changeable and do not


allow duplicates.

May 14, 2024 63


Python Dictionaries

May 14, 2024 64


Python Dictionaries

May 14, 2024 65


Python Dictionaries

May 14, 2024 66


Python Dictionaries

May 14, 2024 67


Python Dictionaries

May 14, 2024 68


Python Dictionaries

May 14, 2024 69

You might also like