Python UNIT 3 19dec
Python UNIT 3 19dec
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
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
Nested Loops
Engineering
Control Statements
Engineering
• Break
• Continue
• Pass
Break Statement
Engineering
for Loop
While Loop
Continue Statement
Engineering
Pass Statement
Engineering
For loop
Strings
Engineering
• Accessing Strings
• Basic Operations
• String slices
• Function and Methods
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])
• %c for character
• %s for strings
Tuples in
Engineering
Python
tuple
Accessing Values in
Engineering
Tuple
• Tuples are immutable which means you cannot update or change the
values of tuple elements
Basic Tuples
Engineering
Operations
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
Lists in Python
Lists in Python
Output:
Dictionary
Accessing Values in
Dictionary
Updating Dictionary
(a) Only one entry per key, if duplicate key is used then
the last assignment wins.
(b) Keys must be immutable
THANK YOU