Please enable JavaScript.
Coggle requires JavaScript to display documents.
Lesson 4 Python essentials - Coggle Diagram
Lesson 4 Python essentials
Datatypes of Python
String
Integer
None
Float
Boolean
Typecasting
Convert one datatype to other
int() or str()
Tuples
Immutable
Enclosed in round brackets ()
any datatype or combination can be include in tuples
eg. tuple1= (1,2.3, "apple", 95, 12, "Jack", (4,5))
indexing
positive
indexing (tuple1[3])
will give output 95
negative
indexing (tuple1[-2])
output "Jack"
slicing
positive
slicing(tuple1[2:5])
slice end position (here 5) not included
will output ("apple",95,12)
negative
slicing(tuple1[-2:-5])
will output ("Jack",12,95)
Lists
Same as tuples
mutable
square brackets
functions
.pop(number)
removes an element on the particular position and shows us its value
list1.pop(1)
output will remove 1st element ie 101 from list and display 101
.remove (xyz)
removes elements
same as append
.insert(tuple)
inserts tuple elements in the list
list1.insert((10,20))
["Mark", 101, 23.6, "test", None, 11,10,20]
.append (xyz)
adds elements
list1.append((10,20))
["Mark", 101, 23.6, "test", None, 11, (10,20)]
list1.append(20)
["Mark", 101, 23.6, "test", None, 11, 20]
list1=["Mark", 101, 23.6, "test", None, 11]
slicing and indexing same as tuples
Dictionaries
set of keys(immutable) and values (mutable and can be of any datatype)
Enclosed in curly brackets{} and key value pairs are separated by colons(:)
dict1={Name: "Shubham", Age:23, Sex: "Male"}
Functions
.values()
dict1.values() will give ["Shubham", 23, "Male"]
.keys()
dict1.keys() will give [Name, Age, Sex]
calling a value
dict1[Name] will give "Shubham"
.update
dict1.update(Age:27)
del
del list1[Age]
Sets
Unordered
Represented in {}
Same as dictionaries but without keys
set1= {1,2,3,4,5,6,7,8}
Functions
Set function can be used to create sets from lists/tuple
set1=set([1,2,3,4,5,6,7,8])
union(|)
set2={1,2,9,10}
set1|set2 will give {1,2,3,4,5,6,7,8,9,10}
intersection(&)
set1&set2 will give {1,2}
contains only unique elements
Operations
in
if an object is present in the list/tuple
+
for concatination
*
for repeating an element
Functions
def my_first_function(argument):return argument
built in sequence functions
enumerate (list)
sorted (list)
reversed(list)
zip(list1,list2)
Control flow
elif
else
If
for
while
continue
break
def try except block