What Will I Learn?
Requirements and Difficulty
Requirements and difficulty are same as Part I. I will leave a link to previous tutorials at the bottom of this post.
Tutorial Contents
Lists:
A list is the ordered collection of any data types of python. It is also a data type. It is enclosed in square brackets '[]' and values are separated by a comma. Indexing in list also starts with zero. List can contain another list inside it.
Some of the inbuilt functions of lists are as follows:
list.append(item) adds an item to the list.
list.count(item) counts the occurrence of given item in the list.
list.remove(item) removes the given item from the list.
info = ['programminghub', 'steemit' , 2, 'posts', 'python']
info.append('useless')
print(info)
info.remove('useless')
print(info)
print(info.count('posts'))
info1 = [['programminghub', 'steemit'] ,[ 2, 'posts', 'python']]
print(info1[0])
Output:
['programminghub', 'steemit', 2, 'posts', 'python', 'useless']
['programminghub', 'steemit', 2, 'posts', 'python']
1
['programminghub', 'steemit']
Tuples:
Tuples are similar to lists. The differnce between tuples and lists are as follows:
| Lists | Tuples |
|---|---|
| enclosed in [] | enclosed in () |
| mutable | immutable |
| ordered | structured |
| usually homogeneous | usually heterogeneous |
| variable length | fixed length |
| can be changed | cannot be changed |
We can perform operations that we did with lists to tuples but the operations that changes or modifies tuples will raise error while executing.
info = ('programminghub', 'steemit' , 2, 'posts', 'python')
print(info.count('posts'))
info1 = (('programminghub', 'steemit') ,( 2, 'posts', 'python'))
print(info1[0])
Output:
1
('programminghub', 'steemit')
But when we try to modify it's values then error occurs.
info = ('programminghub', 'steemit' , 2, 'posts', 'python')
info.append('useless')
print(info)
info.remove('useless')
print(info)
Output Error:
in <module>
info.append('useless')
AttributeError: 'tuple' object has no attribute 'append'
Sets:
Sets are the unordered collections of unique elements. Elements in sets are not repeated. It supports mathematical set operations such as intersection, union, difference, and symmetric difference. The sets are enclosed in curly brackets'{}' or also we can use set() to create it.
x = {'a', 'a', 'b', 'c', 'd', 'e', 'f', 'f', 'f', 'g', 'h'}
print("Sets after removing repeated elements:")
print(x)
y = set("ghijkl")
print(y)
print(x-y)
print(x | y)
print(x ^ y)
Output:
Sets after removing repeated elements:
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'h'])
set(['g', 'i', 'h', 'k', 'j', 'l'])
set(['a', 'c', 'b', 'e', 'd', 'f'])
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'k', 'j', 'l'])
set(['a', 'c', 'b', 'e', 'd', 'f', 'i', 'k', 'j', 'l'])
Loops:
While writing programs there may come a situation where we need to run a block of statement number of times. Loops allow us to execute that block of statement multiple times. The loop statement executes the block of code until the condition is fulfilled. The types of loops in python are: while loop, for loop and nested loops. the three loop control statements in python are: break, continue and pass.
while loop: It executes while given condition is TRUE.
for loop: It iterates over and over until the condition is met.
nested loops: It is one loop inside another loop.
break: It terminates the loop and executes the following statement after the loop.
continue: It continues with the next iteration of the loop.
pass: It does nothing. We need pass when the program demands it syntactically but we don't need to execute anything.
#while loop example:
num = 0
while True:
print(num)
num += 1
if num >= 4:
break
In above code, while loop prints the num which is increased by 1 until the num is less than or equal to five. After that break terminates the loop.
Output:
0
1
2
3
#for loop example
for x in range(5):
if x % 2 == 0:
continue
print(x)
Above code will check the numbers up to range of five whose remainder will be zero when devided by two and continue makes to skip that and remaining number will be printed.
Output:
1
3
For more details visit Python Docs
Above codes can be found on my github link.
Curriculum
Link of Part I