Data Type in Python
Data Type in Python
String Literals
String literals in python are surrounded by either single quotation
marks, or double quotation marks.
'hello' is the same as "hello".
You can display a string literal with the print() function:
Multiline Strings
You can assign a multiline string to a variable by using three quotes:
a = """Python is a programming language.
Python can be used on a server to create web applications..
"""
print(a)
Strings are Arrays
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.
Square brackets can be used to access elements of the string.
Get the character at position 1 (remember that the first character has the
position 0):
Example
a = "Hello, World!"
print(a[1])
Slicing
You can return a range of characters by using the slice syntax.
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 to position 5 (not included):
Example. Result.
b = "Hello, World!" llo
print(b[2:5])
String Length
To get the length of a string, use the len() function.
a = "Hello, World!"
print(len(a))
The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
The replace() method replaces a string with another string:
a = "Hello, World!"
print(a.replace("H", "J"))
Python Lists
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 and unindexed. No duplicate
members.
Dictionary is a collection which is unordered, changeable and indexed.
No duplicate members.
List
A list is a collection which is ordered and changeable. In Python lists are
written with square brackets.
Create a List:
thislist = ["apple", "banana", "cherry"]
print(thislist)
Access Items
You access the list items by referring to the index number:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
Range of Indexes
You can specify a range of indexes by specifying where to start and
where to end the range.
Return the third, fourth, and fifth item:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[2:5])
This example returns the items from the beginning to "orange":
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[:4])
This example returns the items from "cherry" and to the end:
thislist = ["apple", "banana", "cherry", "orange", "kiwi", "melon",
"mango"]
print(thislist[2:])
Using the append() method to append an item:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
Insert an item as the second position:
thislist = ["apple", "banana", "cherry"]
thislist.insert(1, "orange")
print(thislist)
The remove() method removes the specified item:
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
The pop() method removes the specified index, (or the last item if index
is not specified):
You cannot access items in a set by referring to an index, since sets are
unordered the items has no index.
But you can loop through the set items using a for loop, or ask if a
specified value is present in a set, by using the in keyword.
Loop through the set, and print the values:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Result: banana
cherry
apple
Change Items
Once a set is created, you cannot change its items, but you can add new
items.
Add Items
To add one item to a set use the add() method.
To add more than one item to a set use the update() method.
Add an item to a set, using the add() method:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
Result:
{'orange', 'banana', 'apple', 'cherry'}
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.clear()
print(thisdict)
Result:
{}
Nested Dictionaries
A dictionary can also contain many dictionaries, this is called nested
dictionaries.
Create a dictionary that contain three dictionaries:
myfamily = {"child1" : {
"name" : "Emil",
"year" : 2004
},"child2" : {
"name" : "Tobias",
"year" : 2007
},"child3" : {
"name" : "Linus",
"year" : 2011}}
THANK YOU