Python - Self Learning in a Fast & Simple Way
Python - Self Learning Part 2
Python
Strings
String
is a series of characters enclosed within quotes (single, double, or triple
quotes). To access the strings, we use slice operator. String characters start
at index 0 instead of 1, means that first character is at index (place) 0. It
is good to know when you want to determine or access the string characters. We
can concatenate strings in Python, and we use + operator (sign). The asteric *
symbol is used for repeating strings. # is used to write statement/comments or
explanation which will not show in coding result. Please be noted that space
between two characters also considered as a character.
Example:
welcome
= "Welcome Back"
print
(welcome) #to print the complete string
Welcome
Back
E.g.,
print (welcome [0]) # to print the first character of the string.
W
e.g.,
print (welcome [2:9]) # to print from 3rd character to 9th
character but 9th character will not show
lcome
B
e.g.,
print (welcome [4:]) # to print from 5th character of the string
ome
Back
e.g.,
print (welcome * 2) # to print the string two times
Welcome
BackWelcome Back
e.g.,
print (welcome + “Thank You”) # to print a concatenate string
Welcome
BackThank You
In
above examples, we noticed that there are not space in between two words, to
make a space in strings we have to write quote (“ ”).
Python
Tuples
Tuples
are collection of Python data types that cannot be modified or changed but can be
arranged in a specific sequence or order. Tuples allows duplicate items are written
within round brackets () e.g.
Tuple
= (“string001”, “string002”, “string003”, “string004”)
Print
(Tuple)
Result
= (“string001”, “string002”, “string003”, “string004”)
Similar
to Python strings, you can also select a particular string in tuple while
referencing in square brackets []. E.g.
Tuple
= (“string001”, “string002”, “string003”, “string004”)
Print
(Tuple[2]
Result
= (“string003”)
The
concept of negative indexing in Python can also be applied in Tuple as:
Tuple
= (“string001”, “string002”, “string003”, “string004”, “string005”)
Print
(Tuple [-2])
Result
= (“string004”)
We
can also specify a range of indexes in Tuple by giving start and end range. E.g.
Tuple
= (“string001”, “string002”, “string003”, “string004”, “string005”)
Print
(Tuple [1:4])
Result
= (“string002”, “string003”, “string004”)
Please
be noted, 1st item is at position 0 and 4th item in the range is not
included.
We
can also specify a negative range of indexes in Python Tuples. E.g.
Tuple
= (“string001”, “string002”, “string003”, “string004”, “string005”)
Print
(Tuple [-5:-2])
Result
= (“string004”, string002”)
1st
item is at position -1 and final position -5 is not included in the output.
We
cannot change the data value of Python in tuples but can update it. E.g.
x
= ("apple", "banana", "mango",
"orange")
y
= list(x)
y[2]=
"grapes"
x
= tuple(y)
print(x)
result
= ('apple', 'banana', 'grapes', 'orange')
We
can also find out the length of a python tuple by using “len()” function e.g.,
Tuple
= ("apple", "banana", "mango",
"orange")
Print
(len (Tuple))
Result = 4
We
cannot delete any item specifically from the tuple but can delete it entirely
by using “del” keyword to delete the tuple completely. E.g.
We
can join multiple tuples by using ‘+’ logical operator, e.g.
x
= ("apple", "banana", "mango",
"orange")
y
= (1, 2, 3)
z
= x + y
print
(z)
result
= x = ("apple", "banana", "mango",
"orange", 1, 2, 3)
Python Booleans
Booleans
represent one of two values: True or False. It’s used in comparison, and
valuation, verification, and confirmation of two data values. E.g.
Print
(80>70)
True
Print
(70==60)
False
Print
(90<80)
False
Look
at the bool () function, which allows for the comparison of numerical as well
as strings data in ‘True’ or ‘False’ Boolean values as:
Print
(bool (66))
True
Print
(bool (“Good Night”))
True
X
= “Just go with it”
Print
(bool (X))
True
Y
= 4.67
Print
(bool(Y))
True
Please
be noted that:
1-
If a statement has data, it will be
confirmed as ‘True’.
2-
All string data values will be, “True”
unless it is empty.
3-
All numeric values will be treated as
“True” except ‘0’.
4-
Lists, tuples, Sets, dictionaries will
be evaluated as “true” unless empty.
5-
Empty value like (), {}, [], “”, are
“False”, None & 0 will also be “False”.
6-
Any object resulting due to “len”
function that results data value as “0” or “False” is also “False”.
Python Lists
Lists
are collection of data types which can be organized, changed, & include
duplicate values. Lists are written in square brackets [].
e.g.,
x = [“string001”, “string002”, “string003”]
print(x)
we
can select the desired string from lists by calling the position. Like:
x
= [“string001”, “string002”, “string003”, “string004]
print
(x [2])
Outcome
= “string003”
Similarly
negative index in Python list is depicted as:
x
= [“string001”, “string002”, “string003”, “string004”]
print
(x [-3])
outcome
= [“string002”]
Range
of Indexes
x
= [“string001”, “string002”, “string003”, “string004”, “string005”]
print
(x [2:4])
outcome
= [“string003”, “string004”]
if
you donot mention the start range it will take from 0. E.g.,
x
= [“string001”, “string002”, “string003”, “string004”]
print
(x[ :3])
outcome
= [“string001”, “string002”, “string003”]
Same
way if you donot mention end of the range, outcome will be till end of the
list.
x
= [“string001”, “string002”, “string003”, “string004”]
print
(x [1: ]
[“string002”, “string003”, “string004”]
Range of negative Indexes
x
= [“string001”, “string002”, “string003”, “string004”]
print
(x [-3:-1]
[“string002”,
“string003”]
Last
item is at position -1 which is not included.
Change
the data value by referring index number and assigning new value. E.g.
x
= [“string001”, “string002”, “string003”, “string004”]
x
[3] = “surprise”
print(x)
output
= [“string001”, “string002”, “string003”, “surprise”]
length
of a python list
x
= [“string001”, “string002”, “string003”]
print
(len (x))
Outcome
= 3
Adding
new value by using built-in “append ()” method. E.g.
x
= [“string001”, “string002”, “string003”]
x.append
(“surprise”)
print
(x)
outcome
= [“string001”, “string002”, “string003”, “surprise”]
Adding
a new value at a specific position by using “inser ()” method.
x
= [“string001”, “string002”, “string003”]
x.insert
(1, “surprise”)
print
(x)
Outcome
= [“string001”, “surprise”, “string003”]
Copy
an existing python list by using built-in “copy ()” and/or “list ()” method.
x
= [“string001”, “string002”, “string003”]
y
= x.copy ()
print(y)
outcome
= [“string001”, “string002”, “string003”]
x
= [“string001”, “string002”, “string003”]
y
= list(x)
print(y)
outcome
= [“string001”, “string002”, “string003”]
there
are multiple ways to delete items from the list: “remove ()”, “pop ()”, “del
()”, & “clear ()”.
x
= [“string001”, “string002”, “string003”, “string004”]
x.remove
(“string002”
print
(x)
outcome
= [“string001”, “string003”, “string004”]
x
= [“string001”, “string002”, “string003”, “string004”]
x.pop
()
print
(x)
outcome
= x = [“string001”, “string002”, “string003”]
x
= [“string001”, “string002”, “string003”, “string004”]
del
(x[2])
print
(x)
outcome
= [“string001”, “string002”, “string004”]
x
= [“string001”, “string002”, “string003”, “string004”]
del
(x)
print
(x)
outcome
= …..
x
= [“string001”, “string002”, “string003”, “string004”]
x.clear
()
print
(x)
outcome
= [ ]
Concatenation
of Lists
You
can add multiple lists with the use of “+”, “append ()”, and “extend ()”. E.g.,
x
= [“string001”, “string002”, “string003”]
y
= [5, 10, 15]
z
= x + y
print
(z)
output
= [“string001”, “string002”, “string003”, 5, 10, 15]
for
x in y
x
= [“string001”, “string002”, “string003”]
y
= [5, 10, 15]
x.append
(x)
print
(x)
output
= [“string001”, “string002”, “string003”, 5, 10, 15]
x
= [“string001”, “string002”, “string003”]
y
= [5, 10, 15]
x.extend
(y)
print
(x)
output
= [“string001”, “string002”, “string003”, 5, 10, 15]
Python Sets
Sets
are collection of data types that cannot be organized and indexed. No duplicate
items in sets and are shown in curly bracket {}.
Set
= {“pine”, “almond”, “walnut”, “dates”}
Print
(set)
{“pine”,
“almond”, “walnut”, “dates”}
Python
sets are not arranged, so you cannot select the items by referencing the
position same as in tuples and lists. However, the “for” loop can be used in
sets.
We
cannot directly change the value once it is created in python sets. We can use
“add ()” method to add a single item or “update ()” to one or more items to an
already existing set. E.g.,
Set
= {“pine”, “almond”, “walnut”}
Set.add
(“dates”)
Print
(set)
{“pine”, “almond”, “walnut”, “dates”}
Set
= {“pine”, “almond”, “walnut”}
Set.update
(“dates”, “cashews”, “pistachios”)
Print
(set)
{“pine”,
“almond”, “walnut”, “dates”, “cashews”, “pistachios”}
We
can determine the length of set by using “len()” function e.g.,
Set
= {“pine”, “almond”, “walnut”, “dates”, “cashews”, “pistachios”}
Print
(len (set))
Outcome
= 6
To
delete a specific item from the set, “remove ()” and “discard ()” methods can
be used e.g.,
Set
= {“pine”, “almond”, “walnut”, “dates”}
Set.remove
(“walnut”)
Print
(set)
{“pine”,
“almond”, “dates”}
Set
= {“pine”, “almond”, “walnut”, “dates”}
Set.discard
(“almond”)
Print
(set)
{“pine”,
“walnut”, “dates”}
“Pop()”
method used for deleting the last item. As we know that sets are not organized,
so, what system will think last item will be deleted and result will be item
removed e.g.,
Set
= {“pine”, “almond”, “walnut”, “dates”}
A=
set.pop ()
Print
(A)
Print
(set)
{“pine”,
“almond”, “dates”}
“del” keword is used to delete the entire python set e.g.,
Set
= {“pine”, “almond”, “walnut”, “dates”}
Delete
set
Print
(set)
Output
= name ‘set’ is not defined.
“clear
()” method can be used to delete all items from set except variables. E.g.,
Set
= {“pine”, “almond”, “walnut”, “dates”}
Set.clear
()
Print
(set)
Output
= set ()
We
can join multiple data sets by using “union ()” method. Output will be a new
set, combination of both sets. We can use “update ()” method as well to inject
one set to another set. E.g.,
setA
= {“pine”, “almond”, “walnut”, “dates”}
setB
= {3, 15, 20, 40}
setC
= setA.union (setB)
print
(setC)
outcome
= {“pine”, “3”, “almond”, “15”, “walnut”, “20’, “dates”, “40”}
setA
= {“pine”, “almond”, “walnut”, “dates”}
setB
= {3, 15, 20, 40}
setA.update(setB)
print
(setA)
outcome
= {“pine”, “15”, “almond”, “3”, “dates”, “20’, “walnut” “40”}
we
can also use “set ()” set constructor to create a new set e.g.,
setA
= set ((“pine”, “almond”, “walnut”, “dates”))
print
(setA)
Result = {“pine”, “almond”, “walnut”, “dates”}
Functions
Python
consists of several built-in functions to use the integers. It only runs when
it is called. Python library has number of modules and to use the functions on
these modules, we must import the modules. E.g., how to call a function,
Function
name (parameters), Lists – also called as “arrays” helps in grouping various
types of data.
Dictionaries
These
are unordered associative arrays that can be implemented using hash tables. These
are used to store data in key. E.g., how to create, add & delete entries in
dictionary.
#make
phone book: Phonebook = {‘Ali’:12345,\ ‘Maaz’:67894,\ ‘Saba’:67893,\ ‘Bira’:65732}
#add
the person ‘Muhammad’ to the phonebook: Phonebook [‘Muhammad’] = 12375
Del phonebook [‘Saba’]
Comments
Post a Comment