You are on page 1of 49

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

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

April 7, 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.

April 7, 2024 5
Python Variables

• String variables can be declared


either by using single or double
quotes.

April 7, 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.

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

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

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

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

April 7, 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"
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

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

April 7, 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.

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:

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


statements" and loops.

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

Other String functions:


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

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

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

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

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

April 7, 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':
April 7, 2024 43
Python List (Access List Items)

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

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

April 7, 2024 48
Python Tuple

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

April 7, 2024 49

You might also like