Python Unit 3
Python Unit 3
Strings
Definition & Accessing :
Like many other popular programming languages, strings in Python are arrays of bytes
representing Unicode characters.
However, Python does not have a character data type, a single character is simply a string with a
length of 1.
Since strings are arrays, we can loop through the characters in a string, with a for loop.
Ex:
for x in "banana":
print(x)
age = 36
txt = "My name is John, I am " + age
print(txt)
** it raises error.
** in python the functions defined in a class are called methods and that are defined outside class
(directly with in python runtime environment) are called functions. So the functions are called
directly (without object) and the methods are called with an object.
These are list of methods of string class.
endswith() Returns true if the string ends with the specified value
find() Searches the string for a specified value and returns the position of
where it was found
index() Searches the string for a specified value and returns the position of
where it was found
isalpha() Returns True if all characters in the string are in the alphabet
islower() Returns True if all characters in the string are lower case
isupper() Returns True if all characters in the string are upper case
startswith() Returns true if the string starts with the specified value
swapcase() Swaps cases, lower case becomes upper case and vice versa
Specify the start index and the end index, separated by a colon, to return a part of the string
Get the characters from position 2, and all the way to the end:
b = "Hello, World!"
print(b[2:])
---
Q. Write about strings in python
---
Lists
Introduction:
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3
are Tuple, Set, and Dictionary, all with different qualities and usage.
List items are indexed, the first item has index [0], the second item has index [1] etc.
Lists are ordered, it means that the items have a defined order, and that order will not change. If
you add new items to a list, the new items will be placed at the end of the list. So these maintain
the order in which the data is stored in it. The list is changeable, meaning that we can change,
add, and remove items in a list after it has been created.
we can store different data type elements in a list like strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male"]
* we can also use the list() constructor to make a List, but it takes only one argument.
thislist = list(("apple", "banana", "cherry"))
OR
thislist = list(["apple", "banana", "cherry"])
OR
thislist = ["apple", "banana", "cherry"]
for i in range(len(thislist)):
print(thislist[i])
we can also sort lists with the functions of List class :
sorting ascending order (by default)
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort()
print(thislist)
sorting in descending order:
thislist = ["orange", "mango", "kiwi", "pineapple", "banana"]
thislist.sort(reverse = True)
print(thislist)
for x in list2:
list1.append(x)
print(list1)
3. list1 = ["a", "b" , "c"]
list2 = [1, 2, 3]
list1.extend(list2)
print(list1)
Operations :
We can add items to list, modify data in a list and remove items in a list.
Modifying items :
Method Description
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
---
Q. Explain about lists in python
---
Tuples
introduction, accessing tuple
Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3
are List, Set, and Dictionary, all with different qualities and usage.
Tuple items are ordered, unchangeable, and allow duplicate values. Tuple items are indexed, the
first item has index [0], the second item has index [1] etc. tuples are ordered, it means that the
items have a defined order, and that order will not change. Tuples are unchangeable, meaning
that we cannot change, add or remove items after the tuple has been created.
thistuple = ("apple",) this is treated as a tuple object, and a tuple must have more than one
element.
thistuple = ("apple"), this is treated as a string object and NOT as tuple as it has only one
element.
or
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
as tuples are not modifiable, if we get a requirement of modifying values of a tuple, we can store
tuple into a list, do modification in that list and then store values back into the tuple as shown
below:
test=list(fruits)
test[1]='tested value'
fruits=tuple(test)
print('after modification',fruits)
---
Q. explain tuples in python
---
Dictionaries
introduction, accessing values in dictionaries
A dictionary is a collection which is ordered, changeable and do not allow duplicates. In python,
in old versions the dictionaries were un-ordered. Dictionaries are written with curly brackets, and
have keys and values.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
output :
Dictionary items are presented in key:value pairs, and can be referred to by using the key name.
** in lists or tuples, we use index number to refer each item. But in dictionaries, we use key to
refer each item instead of index number.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
x = car.values()
print(x) #before the change
car["year"] = 2020
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"color": "red"})
we can use del, pop(), popitem(), clear() to remove items of the dictionary as we can do with
tuples.
---
Q. write about dictionaries of python
---