Unit4 Part1
Unit4 Part1
Ex3: print "My name is %s age is %d“ % (‘xyz‘,20)) // print multiple values
Output: My name is xyz age is 20
Print value of variable
Ex:
name=“raj”
print "My name is %s “ % name) // %s will be replaced by value of variable name
Python Strings: : Format Specifiers
Sr.No Format Symbol & Conversion
1 %c character
2 %s string conversion via str() prior to formatting
3 %i signed decimal integer
4 %d signed decimal integer
5 %u unsigned decimal integer
6 %o octal integer
7 %x hexadecimal integer (lowercase letters)
4. split(separator_char): Splits the string at the specified separator, and returns a list
Ex: s=“hi all” s.split() gives list [‘hi’, ‘all’]
8. find(substring): if substring is present,returns the index where substring occurs. If not, returns -1
Ex: s=“hi how are you?” s.find(‘how’) gives 3, s.find(‘wel’) gives -1
12. islower(): Returns True if all characters in the string are lower case
Ex: s=‘Hello’ s.islower() gives False
Python Strings: built in functions
13. center(space,pad_char): center align the string, using a specified character (space is default) as the fill character.
Ex: s=“welcome” s.center(10) gives ‘ welcome ’
8. find(substring): if substring is present,returns the index where substring occurs. If not, returns -1
Ex: s=“hi how are you?” s.find(‘how’) gives 3, s.find(‘wel’) gives -1
12. islower(): Returns True if all characters in the string are lower case
Ex: s=‘hello’ s.islower() gives True
Python Strings: operations
1. String Concatenation: use + operator
S1=“hi”
S2=“hello”
S3=s1+s2 gives “hi hello”
S[:2] gives “hi” // if 1st index is omitted then it extracts from the beginning.
S[10:] gives “How are you” // if 2nd index is omitted then it extracts upto the end.
Python Strings: operations
Negative indexing: extract from the end
str_rev=s[::-1]
If s==str_rev:
print(“string is palindrome”)
Else:
print(“not palindrome”)
List
• Lists are used to store multiple items in a single variable.
• Store dissimilar type of data
• used to store collections of data
• ordered, mutable, and allow duplicate values
• Elements are referenced by index position.
• Index begins at 0. index of last element is 1- total no of elements in list
Creating list:
Creating list:
Method1. Enlist elements in square brackets
e.g. fruits= ["apple", "banana", "cherry“]
Negative Indexing
• Negative indexing starts from the end.
• -1 refers to the last item, -2 refers to the second last item etc.
• Ex: courses[-1] gives “TCS” , courses[-2] gives “Java” Range of Indexes
Range of Index
• You can specify a range of indexes by slice operator.
• All the values within the range will be returned.
• Ex: course[0:2] gives [“c”, “C++”]
List operations
Length of list
Syntax: len(listobject)
Ex: fruits = ["apple", "banana", "cherry"]
print(len(fruits))
Append Items
Syntax: listobject.append(item)
Ex: fruits.append(“guava”)
Copy list
Syntax: listobject.copy()
Ex: fruitlist2=fruits.copy() // new list with same items is generated.
Join items of two Lists
Syntax: listobject1.extend(listobject2)
Ex: fruits.extend(numbers) gives ["apple", "cherry“, "banana“, 10,5,34,56,45]
List Traversal
Traverse a list: use loop
for x in fruits:
print(x)
Ex: make the squares of numbers in a given list and store in another list.
numbers=[2,6,-16,4]
squares = [x*x for x in numbers]
Ex. Create a list containing only even numbers from another list
Even= [x for x in numbers if x%2==0]
[('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)]
[('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)] Dictionary
• Dictionaries are used to store data values in {key:value} pairs.
[('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)]
• ordered,
[('brand', 'Ford'), ('model', 'Mustang'), ('year',
mutable 1964)]
Method1. create
[('brand', new ('model',
'Ford'), key and'Mustang'),
assign value to 1964)]
('year', it.
Syntax: dictobject[“newkey”]=value
Ex: cars[“price”]= 1000000 // create price key and store value 1000000
Method2. use update() method. Provide {key:val} pair to update method.
Cars.update({“price”:1000000})
students ={
[('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)]
“student1" : { 1. Print the name of student1
"name" : "Emili",
“PRN" : 12345, students[“student1”][“name”]
“gender”:”female”
2. Print info of all students
“grade” :”O”
}, For student in students:
“student2" : { print(students[student])
"name" : “akash",
“PRN" : 12346, 3. Print grade of student with PRN 12346
“gender”:”male”
for student in students:
“grade” :”O”
if students[student]["PRN"]==12347:
},
print(students[student]["grade"])
“student3" : {
"name" : “seema", 4. Add new student
“PRN" : 12347, stud={"name":"akanksha","PRN":12348,"gender":"female","grade" :"O"}
“gender”:”female” students.update({"student4":stud})
“grade” :”A”
}} 5. Delete student 3 record
students.pop("student3")