0% found this document useful (0 votes)
40 views

Python UNIT 3 19dec

The document is about introducing Python programming using for loops. It discusses the syntax of for loops in Python and provides examples of using for loops to iterate through lists, tuples and strings. Some key points covered include: - Using for loops to iterate sequentially through a sequence like a list or string - Nested for loops to iterate through multiple sequences - Control statements like break, continue and pass that can be used within for loops - Using strings in Python like initializing, accessing characters, slicing strings, and built-in string functions.

Uploaded by

KAVYA GBAGALI
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)
40 views

Python UNIT 3 19dec

The document is about introducing Python programming using for loops. It discusses the syntax of for loops in Python and provides examples of using for loops to iterate through lists, tuples and strings. Some key points covered include: - Using for loops to iterate sequentially through a sequence like a list or string - Nested for loops to iterate through multiple sequences - Control statements like break, continue and pass that can be used within for loops - Using strings in Python like initializing, accessing characters, slicing strings, and built-in string functions.

Uploaded by

KAVYA GBAGALI
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/ 41

RV College of

Engineering

Introduction
INTRODUCTIONto TO PYTHON PROGRAMMING

UNIT III
Programming
UNIT-1
Prof. Narasimha Swamy S
Department of AIML
RV College of Engineering
Bengaluru-59 Go, Change the World
RV College of

For loops
Engineering

• A for loop is used for iterating over a particular sequence (can be a list, a


tuple, a dictionary, a set, or a string).
• This is less like the for keyword in other programming languages
• Similar to an iterator method as found in other object-orientated
programming languages.
• With the for loop a set of statements, once for each item in a list, tuple, set
etc can be executed.
• Used for sequential traversal.

29/07/2023 Dr.Kavitha S N,Dept. of ISE 2


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

A simple ‘for’ loop


Engineering

Syntax:
for iterator_var in sequence:
statements(s)
Example 1: output: 0 1 2 3
n=4
for i in range(0, n):
    print(i)
Example 2: output: 4 3 2 1 stop!
for i in [4, 3, 2, 1] :
print(i)
print(‘stop!')
29/07/2023 Dr.Kavitha S N,Dept. of ISE 3
22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Example 3
friends = ['Joseph', 'Glenn', 'Sally']
for friend in friends :
print(‘Hello:', friend)
print('Done!’)
Output:
Hello: Joseph
Hello: Glenn
Hello: Sally

Done!
29/07/2023 Dr.Kavitha S N,Dept. of ISE 4
22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Example 4

29/07/2023 Dr.Kavitha S N,Dept. of ISE 5


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Nested Loops
Engineering

29/07/2023 Dr.Kavitha S N,Dept. of ISE 6


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Control Statements
Engineering

• Break
• Continue
• Pass

29/07/2023 Dr.Kavitha S N,Dept. of ISE 7


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Break Statement
Engineering

for Loop
While Loop

29/07/2023 Dr.Kavitha S N,Dept. of ISE 8


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Continue Statement
Engineering

While Loop for Loop

29/07/2023 Dr.Kavitha S N,Dept. of ISE 9


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Pass Statement
Engineering

For loop

Empty For loop

Empty For loop

29/07/2023 Dr.Kavitha S N,Dept. of ISE 10


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Strings
Engineering

• Accessing Strings
• Basic Operations
• String slices
• Function and Methods

29/07/2023 Dr.Kavitha S N,Dept. of ISE 11


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Strings
Engineering

• String Initialization
a='Welcome, to RVCE!'
b = "Welcome, to RVCE!"
c="""Welcome,
to RVCE!""“
• Accessing characters in a string
a[1]
29/07/2023 Dr.Kavitha S N,Dept. of ISE 12
22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Strings
Slicing the Strings
Engineering

Substring
a='Welcome, to RVCE!‘
Print(a)
print(a[2:5])
print(a[2:])
print(a[:3])

29/07/2023 Dr.Kavitha S N,Dept. of ISE 13


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

String- In built functions


Engineering

strip() -- removes whitespace from the beginning or


end
a = “ Welcome, to RVCE! “
print(a.strip())

29/07/2023 Dr.Kavitha S N,Dept. of ISE 14


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

String- In built functions

• len() -- returns the length of a string


a = “ Welcome, to RVCE! “
print(len(a))

29/07/2023 Dr.Kavitha S N,Dept. of ISE 15


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

String- In built functions

• split() -- splits the string into substrings if it finds instances of the


separator

29/07/2023 Dr.Kavitha S N,Dept. of ISE 16


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

String- In built functions


lower() -- returns the lower case of
the string

upper() -- returns the upper case


of the string

lower() -- returns the replaced


string

29/07/2023 Dr.Kavitha S N,Dept. of ISE 17


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

String manipulation functions


• capitalize() Capitalizes first letter of string
• center (width, fillchar) Returns a space-padded string with
the original string centered to a total of width columns.
• count(str, beg= 0,end=len(string)) Counts how many times
str occurs in string or in a substring of string if starting index
beg and ending index end are given
• endswith(suffix, beg=0, end=len(string)) Determines if
string or a substring of string (if starting index beg and
ending index end are given) ends with suffix; returns true if
so and false otherwise
• Find (str, beg=0 end=len(string))Determine if str occurs in
29/07/2023
string Dr.Kavitha S N,Dept. of ISE 18
22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

String manipulation functions


• isalnum()Returns true if string has at least 1 character and all
characters are alphanumeric and false otherwise.
• isdigit()Returns true if string contains only digits and false otherwise.
• islower()Returns true if string has at least 1 cased character and all
cased characters are in lowercase and false otherwise.
• isspace()Returns true if string contains only whitespace characters and
false otherwise.

29/07/2023 Dr.Kavitha S N,Dept. of ISE 19


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

String Formatting Operator

• %c for character

• %s for strings

• %o for octal numbers

• %x for hexadecimal numbers

• %f for floating point numbers

29/07/2023 Dr.Kavitha S N,Dept. of ISE 20


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Tuples in
Engineering

Python

• A tuple is a collection which is ordered and unchangeable.


• Tuples are written with round brackets.

29/07/2023 Dr.Kavitha S N,Dept. of ISE 21


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
Creating
RV College of
Engineering

tuple

29/07/2023 Dr.Kavitha S N,Dept. of ISE 22


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Accessing Values in
Engineering

Tuple

29/07/2023 Dr.Kavitha S N,Dept. of ISE 23


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Updating values in Tuple

• Tuples are immutable which means you cannot update or change the
values of tuple elements

29/07/2023 Dr.Kavitha S N,Dept. of ISE 24


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Updating values in Tuple

29/07/2023 Dr.Kavitha S N,Dept. of ISE 25


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Delete Tuple Elements

29/07/2023 Dr.Kavitha S N,Dept. of ISE 26


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Basic Tuples
Engineering

Operations

29/07/2023 Dr.Kavitha S N,Dept. of ISE 27


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Indexing, Slicing, and Matrixes

29/07/2023 Dr.Kavitha S N,Dept. of ISE 28


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Built-in Tuple Functions

29/07/2023 Dr.Kavitha S N,Dept. of ISE 29


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Lists in Python
• Lists are used to store multiple items using a single variable.
• Lists is a built-in data types
• Lists are used to store collections of data,
• Similar to lists are Tuple, Set, and Dictionary
• 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]  and so on
• List items can be of any data type

29/07/2023 Dr.Kavitha S N,Dept. of ISE 30


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Lists in Python

list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]


tinylist = [123, 'john’]
print (list) # Prints complete list
print (list[0]) # Prints first element of the list
print (list[1:3]) # Prints elements starting from 2nd till 3rd
print (list[2:]) # Prints elements starting from 3rd element
print (tinylist * 2) # Prints list two times
print (list + tinylist) # Prints concatenated lists

29/07/2023 Dr.Kavitha S N,Dept. of ISE 31


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Lists in Python

Output:

['abcd', 786, 2.23, 'john', 70.2]


abcd
[786, 2.23]
[2.23, 'john', 70.2]
[123, 'john', 123, 'john’]
['abcd', 786, 2.23, 'john', 70.2, 123, 'john']

29/07/2023 Dr.Kavitha S N,Dept. of ISE 32


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Dictionary

• Key is separated from its value by a colon (:)


• Items are separated by commas
• Dictionary is enclosed in curly braces
• Keys are unique within a dictionary while values may not be.

29/07/2023 Dr.Kavitha S N,Dept. of ISE 33


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Accessing Values in
Dictionary

29/07/2023 Dr.Kavitha S N,Dept. of ISE 34


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Updating Dictionary

29/07/2023 Dr.Kavitha S N,Dept. of ISE 35


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Delete Dictionary Elements

29/07/2023 Dr.Kavitha S N,Dept. of ISE 36


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Clear Dictionary elements

29/07/2023 Dr.Kavitha S N,Dept. of ISE 37


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Properties of Dictionary Keys

(a) Only one entry per key, if duplicate key is used then
the last assignment wins.
(b) Keys must be immutable

29/07/2023 Dr.Kavitha S N,Dept. of ISE 38


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

Built-in Dictionary Functions

• len(dict) Gives the total length


of the dictionary

• str(dict) Produces a string


representation of dictionary

• type(variable) Returns the type


of the passed variable.

29/07/2023 Dr.Kavitha S N,Dept. of ISE 39


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of

Built-in dictionary methods


Engineering

29/07/2023 Dr.Kavitha S N,Dept. of ISE 40


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World
RV College of
Engineering

THANK YOU

29/07/2023 Dr.Kavitha S N,Dept. of ISE 41


22PL1B01-Introduction to Python Programming Department of Artificial Intelligence and Machine Learning Go, Change the World

You might also like