1 Data fundamentals in Python
1 Data fundamentals in Python
Variable Assignment
Regular mathematical operators (+, -, *, / and power) can be performed in code cells.
Out[1]: 7.0
Out[2]: 100
The results from the above operations can not be reused in subsequent codes because
they were not assigned any name. To use them elsewhere will require to assign an
operation to a variable. For example
In [3]: x = (10 * 2 + 5 - 4) / 3
y = x**2 + 2*x - 5
To write an output to the screen, we can type the variable name or use the print()
function .
In [4]: x
Out[4]: 7.0
In [5]: y
Out[5]: 58.0
In [6]: # This will only output the last variable in the cell
x
y
Out[6]: 58.0
7.0
58.0
Consider the values of x and y that we printed in the last example. It will be more
intuitive to include some descriptions to the outputs. We can use a string to include
some descriptions in the print function.
x = 7.0
y = 58.0
In [9]: x, y, z= 1,2,3
In [10]: x
Out[10]: 1
In [11]: x,y,z
Out[11]: (1, 2, 3)
Data Types
The following common data types are recognised by python:
In [12]: type("Australia")
Out[12]: str
In [13]: type("4")
Out[13]: str
In [14]: type(4)
Out[14]: int
In [15]: type(3.14567)
Out[15]: float
In [16]: my_Float=3.14567
my_Float
Out[16]: 3.14567
In [17]: int(my_Float)
Out[17]: 3
In [18]: str(my_Float)
Out[18]: '3.14567'
In [19]: float(3)
Out[19]: 3.0
List [ ]
In [20]: # Define a list: Mehtod 1:
myList=[1,2,3]
myList
Out[20]: [1, 2, 3]
Out[21]: [1, 2, 3]
In [23]: # Hashability: Y
myList1[4]
Out[23]: 'Jh'
In [24]: myList1[5][3]
Out[24]: 9
In [25]: myList1[0:4]
Out[25]: [1, 3, 2, 2]
In [26]: myList1[1:6:3]
In [27]: myList_2d=[[1,2,3],[4,5,6],[7,8,9]]
myList_2d
In [28]: myList_2d[0]
Out[28]: [1, 2, 3]
In [29]: myList_2d[1][2]
Out[29]: 6
In [30]: # Mutability: Y
myList1=[1,3,2,2,"Jh",[5,7]]
myList1[2]="New-Value"
myList1
In [31]: myList1[0:2]=[99,999,9999]
myList1
In [32]: myList1[0:2]=[99]
myList1
Tuple ( )
In [33]: # Define a tuple: Mehtod 1:
myTuple=(1,2,3)
myTuple
Out[33]: (1, 2, 3)
Out[34]: (1, 2, 3)
In [36]: # Hashability: : y
myTuple1[2]
Out[36]: 2
In [37]: myTuple1[5][1]
Out[37]: 7
In [38]: # Mutability: N
#myTuple1[2]="New-Value" # This will results in error
#myTuple1
Set { }
In [39]: # Define a set: Mehtod 1:
mySet={2,3,1}
mySet
Out[39]: {1, 2, 3}
Out[40]: {1, 2, 3}
In [42]: # Example
myset2=set([1, 3, 'New-Value', 2,2, 'Jh'])
myset2
In [43]: # Hashability: : N
#mySet1[2] # This will results in error
In [44]: # Mutability: Y
#mySet1[2]="New-Value"
#mySet1 # This will results in error
The error doesn't mean that sets are immutable. It is still related to the fact that sets are
unhashable. To show that sets are mutable (unless frozen), let's apply the remove &
add methods to achieve the same result we wanted
In [45]: mySet1.add(999)
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 6/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
mySet1
Dictionery {key:value}
In [51]: # Define a dictionery: Mehtod 1:
myDict1={"Jhon":36,"Archer":25,"Charlie":40}
myDict1
Each of the dictionery attributes can be converted to list if necessary as illustrated below:
In [53]: # Dic_keys
Names=list(myDict2.keys())
Names
In [54]: # Dic_values
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 7/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
age=list(myDict2.values())
age
In [56]: # Hashablity: : Y
# Dictionary_name['key']=value
myDict3['Jam']
Out[56]: 1
In [57]: myDict3['Fam']
In [58]: myDict3['Fam'][0]
Out[58]: 5
In [59]: # Mutablity: Y
# 1 changing a value
# Dictionary_name['key'] = new_value
myDict3['Jam']=1000
myDict3
Out[59]: {'Jam': 1000, 'Fam': [5, 50], 'Dam': (1, 2, 2), 3: 'Sam'}
In [60]: myDict3['Fam'][0]=5000
myDict3
Out[60]: {'Jam': 1000, 'Fam': [5000, 50], 'Dam': (1, 2, 2), 3: 'Sam'}
In [61]: # Mutablity: Y
# 2 adding a new value
# Dictionary_name['new_key'] = new_value
myDict3['Arther']="100"
myDict3
Out[61]: {'Jam': 1000, 'Fam': [5000, 50], 'Dam': (1, 2, 2), 3: 'Sam', 'Arther': '100'}
In [66]: myDict=dict(zip(myList,myList))
myDict
Out[5]: [1, 2, 3]
In [7]: a,b,c=my_list
In [8]: a
Out[8]: 1
In [9]: a,c
Out[9]: (1, 3)
Tuple ( )
In [31]: my_tuple=[1,2,3]
my_tuple
Out[31]: [1, 2, 3]
In [32]: a,b,c=my_tuple
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 9/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
In [33]: a
Out[33]: 1
In [34]: a,c
Out[34]: (1, 3)
Dictionery {key:value}
keys
In [35]: dic={"Sam": 1, "Bam": 2, "Fam":3}
In [36]: a,b,c=dic
In [37]: a
Out[37]: 'Sam'
In [17]: a,c
values
In [19]: a,b,c=dic.values()
In [20]: a
Out[20]: 1
In [21]: a,c
Out[21]: (1, 3)
items
In [22]: a,b,c=dic.items()
In [23]: a
Out[23]: ('Sam', 1)
In [24]: a,c
String
In [27]: string="good"
In [28]: a,b,c,d=string
In [29]: a
Out[29]: 'g'
In [30]: a,c
Python indexing typically goes from left to right and starts at 0 in unit steps. Right to
left indexing is also allowed and starts at -1 in steps of -1 as illustrated below:
In [67]: # Example 1
myString="Australia"
Out[68]: 'A'
In [69]: # to get the last letter (or the first letter from right)
myString[-1]
Out[69]: 'a'
Slicing: name[start:stop:steps].
Out[70]: 'Austr'
In [71]: myString[0:5]
Out[71]: 'Austr'
In [72]: myString[:5]
Out[72]: 'Austr'
In [73]: myString[5:]
Out[73]: 'alia'
In [74]: myString[-1:-10:-1]
Out[74]: 'ailartsuA'
In [75]: # Example 2
myList=[1,3,4,5,6,7,8,9,10]
myList
In [76]: myList[0]
Out[76]: 1
In [77]: myList[-1]
Out[77]: 10
In [78]: myList[0:5:3]
Out[78]: [1, 5]
Append method
In [79]: myList=[1,2,3]
myList
Out[79]: [1, 2, 3]
In [80]: myList.append(4)
myList
Out[80]: [1, 2, 3, 4]
In [81]: myList=[1,2,3]
myList.append([4,5])
myList
Extend method
In [82]: myList=[1,2,3]
myList.extend([4,5])
myList
Out[82]: [1, 2, 3, 4, 5]
Insert method
In [83]: myList=[1,2,3]
myList.insert(1,10)
myList
In [84]: myList=[1,2,3]
myList.insert(1,[4, 5])
myList
Remove method
In [85]: myList=[1,2,2,3,2]
myList.remove(2)
myList
Out[85]: [1, 2, 3, 2]
Pop method
In [86]: myList=[1,2,3]
myList.pop(1)
myList
Out[86]: [1, 3]
Clear method
In [87]: myList=[1,2,3]
myList.clear()
myList
Out[87]: []
Sort method
In [88]: myList=[3,1,2]
myList.sort()
myList
Out[88]: [1, 2, 3]
Reverse method
In [89]: myList=[3,1,2]
myList.reverse()
myList
Out[89]: [2, 1, 3]
Out[90]: [3, 2, 1]
Out[91]: [3, 2, 1]
Index method
In [92]: myList=[1,2,3,"HHH"]
myList.index("HHH")
Out[92]: 3
Count method
In [93]: myList=[1,2,2,3]
myList.count(2)
Out[93]: 2
Copy method
In [94]: myList=[1,2,3]
myList_copy=myList
myList_copy.remove(2)
myList_copy
Out[94]: [1, 3]
In [95]: myList
Out[95]: [1, 3]
You observe that myList has also changed. This because setting myList_copy equal to
myList did not create a new copy but rather referencing the original list. To make a
distinct copy of a list, we need to use the copy method.
Out[96]: [1, 3]
In [97]: myList
Out[97]: [1, 2, 3]
In [ ]:
class list(object)
| list(iterable=(), /)
|
| Built-in mutable sequence.
|
| If no argument is given, the constructor creates a new empty list.
| The argument must be an iterable if specified.
|
| Methods defined here:
|
| __add__(self, value, /)
| Return self+value.
|
| __contains__(self, key, /)
| Return key in self.
|
| __delitem__(self, key, /)
| Delete self[key].
|
| __eq__(self, value, /)
| Return self==value.
|
| __ge__(self, value, /)
| Return self>=value.
|
| __getattribute__(self, name, /)
| Return getattr(self, name).
|
| __getitem__(...)
| x.__getitem__(y) <==> x[y]
|
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 16/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
| __gt__(self, value, /)
| Return self>value.
|
| __iadd__(self, value, /)
| Implement self+=value.
|
| __imul__(self, value, /)
| Implement self*=value.
|
| __init__(self, /, *args, **kwargs)
| Initialize self. See help(type(self)) for accurate signature.
|
| __iter__(self, /)
| Implement iter(self).
|
| __le__(self, value, /)
| Return self<=value.
|
| __len__(self, /)
| Return len(self).
|
| __lt__(self, value, /)
| Return self<value.
|
| __mul__(self, value, /)
| Return self*value.
|
| __ne__(self, value, /)
| Return self!=value.
|
| __repr__(self, /)
| Return repr(self).
|
| __reversed__(self, /)
| Return a reverse iterator over the list.
|
| __rmul__(self, value, /)
| Return value*self.
|
| __setitem__(self, key, value, /)
| Set self[key] to value.
|
| __sizeof__(self, /)
| Return the size of the list in memory, in bytes.
|
| append(self, object, /)
| Append object to the end of the list.
|
| clear(self, /)
| Remove all items from list.
|
| copy(self, /)
| Return a shallow copy of the list.
|
| count(self, value, /)
| Return number of occurrences of value.
|
| extend(self, iterable, /)
| Extend list by appending elements from the iterable.
|
| index(self, value, start=0, stop=9223372036854775807, /)
| Return first index of value.
|
| Raises ValueError if the value is not present.
|
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 17/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
| insert(self, index, object, /)
| Insert object before index.
|
| pop(self, index=-1, /)
| Remove and return item at index (default last).
|
| Raises IndexError if list is empty or index is out of range.
|
| remove(self, value, /)
| Remove first occurrence of value.
|
| Raises ValueError if the value is not present.
|
| reverse(self, /)
| Reverse *IN PLACE*.
|
| sort(self, /, *, key=None, reverse=False)
| Sort the list in ascending order and return None.
|
| The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
| order of two equal elements is maintained).
|
| If a key function is given, apply it once to each list item and sort them,
| ascending or descending, according to their function values.
|
| The reverse flag can be set to sort in descending order.
|
| ----------------------------------------------------------------------
| Static methods defined here:
|
| __new__(*args, **kwargs) from builtins.type
| Create and return a new object. See help(type) for accurate signature.
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| __hash__ = None
In [99]: #help(set)
Type( )
In [100… my_num=[1,2,3]
type(my_num)
Out[100… list
In [101… type((1,2,3))
Out[101… tuple
In [102… type({2,3,1})
Out[102… set
In [103… type({"Jhon":36,"Archer":25})
Out[103… dict
Len( )
This is used to obtain the number of elements in a data container
In [104… myList
Out[104… [1, 2, 3]
In [105… len(myList)
Out[105… 3
In [106… len("Australia")
Out[106… 9
Range( )
Range is an immutable sequence of integers.
The output does not have much meaning. Let's use the list function to obtain a clearer
output.
Out[108… [0, 1, 2, 3, 4]
In [26]: range(10)
Slice( )
slice(start, stop, step)
print(a[:5])
print(a[slice(5)])
print("\n"*2)
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 19/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
print(a[2:5])
print(a[slice(2, 5)])
Sum( )
In [14]: # sum(iterable, start)
a = [1, 10, 9, 30]
print(sum(a))
print(sum(a, 40))
50
90
Min( )
min(item, item , item, or iterator)
-50
-100
Osama
Max( )
max(item, item , item, or iterator)
30
100
Z
Map( )
[1] Map Take A Function + Iterator: map (Function, Iterator). </font>
[2] Map Called Map Because It Map The Function On Every Element
def formatText(text):
return text.strip().capitalize()
myFormatedData
print(name)
Osama
Ahmed
Sayed
print(name)
Osama
Ahmed
Sayed
Filter( )
[1] Filter Take A Function + Iterator filter (Function, Iterator). </font>
[5] Filter Out All Elements For Which The Function Return True
In [87]: # Example 1
def checkNumber(num):
list(myResult)
In [93]: # Example 2
def checkName(name):
return name.startswith("O")
In [91]: "Omer".startswith("O")
Out[91]: True
In [92]: "Ahmed".startswith("O")
Out[92]: False
Ahmed
Ameer
Reduce( )
[1] Reduce Take A Function + Iterator filter (Function, Iterator). </font>
[2] Reduce Run A Function On FIrst and Second Element And Give Result
[5] Till One ELement is Left And This is The Result of The Reduce
Out[95]: 120
reduce(sumAll, numbers)
Out[96]: 120
Out[98]: 120
Enumerate( )
enumerate(iterable, start=0)
mySkillsWithCounter = enumerate(mySkills)
(0, 'Html')
(1, 'Css')
(2, 'Js')
(3, 'PHP')
mySkillsWithCounter = enumerate(mySkills,10)
(10, 'Html')
(11, 'Css')
(12, 'Js')
(13, 'PHP')
mySkillsWithCounter = enumerate(mySkills)
print(f"{counter} - {skill}")
0 - Html
1 - Css
2 - Js
3 - PHP
Sorted( )
sorted(iterable)
In [1]: data=[4,2,15,8]
sorted(data)
Out[3]: [15, 8, 4, 2]
Reversed( )
reversed(iterable)
Round( )
Nearest digit
99
100
99.55
99.554
99.56
Abs( )
file:///C:/Users/faaiz/Downloads/1 Data fundamentals in Python_ Faaiz Alshajalee (1).html 24/39
22/02/2022, 22:42 1 Data fundamentals in Python_ Faaiz Alshajalee
In [37]: print(abs(100))
print(abs(-100))
print(abs(10.19))
print(abs(-10.19))
100
100
10.19
10.19
pow( )
Power
32
2
Out[41]: 3.2
print( )
In [31]: # separetor
print("Hello Osama How Are You")
print("Hello", "Osama", "How", "Are", "You")
print("-"*50)
In [34]: # End
print("First Line", end=" ")
print("Second Line")
print("Third Line")
First Line
Second Line
Third Line
All( )
In [2]: # All Elements Is True
x = [1, 2, 3, 4]
all(x)
Out[2]: True
Out[4]: False
Out[5]: False
if all(x):
else:
Any( )
In [8]: x = [3, 0, []]
any(x)
Out[8]: True
Out[9]: False
if any(x):
else:
Bin( )
Binery
In [10]: bin(100)
Out[10]: '0b1100100'
ID( )
Memory ID
In [11]: a = 1
b = 2
print(id(a))
print(id(b))
140729271985952
140729271985984
Built-in ( )
String Formatting
Method 1: f-formatting
{} : place holder
In [109… # Example 1
name = "Shola"
age = 35
position = "secretary"
New_string=f"The current {position} of ABC is a {age} year old guy named {name}"
New_string
Out[109… 'The current secretary of ABC is a 35 year old guy named Shola'
In [110… print(f"The current {position} of ABC is a {age} year old guy named {name}")
Method 2: %-formatting
%: place holder
In [111… # Example 1
name = "Shola"
age = 35
position = "secretary"
New_string="The current %s of ABC is a %d year old guy named %s" %(position, age, name)
# s: string, d:digit, f:float
New_string
Out[111… 'The current secretary of ABC is a 35 year old guy named Shola'
In [112… # Example 2
diameter = 3 # cm
New_string
Out[113… 'The current secretary of ABC is a 35 year old guy named Shola'
In [114… New_string="The current {:s} of ABC is a {:f} year old guy named {:s}".format(position,
New_string
Out[114… 'The current secretary of ABC is a 35.000000 year old guy named Shola'
Out[115… 'The current Shola of CURTIN-EAGE is a 35 year old guy named secretary'
In [116… New_string="The current {2:s} of CURTIN-EAGE is a {1:f} year old guy named {0:s}".forma
New_string
Out[116… 'The current Shola of CURTIN-EAGE is a 35.000000 year old guy named secretary'
Out[117… 10.142
In [ ]:
Truncate string
In [118… myLongString='The current secretary of ABC is a 35 year old guy named Shola'
"Short string is %s" %myLongString
Out[118… 'Short string is The current secretary of ABC is a 35 year old guy named Shola'
Out[120… 'Short string is The current secretary of ABC is a 35 year old guy named Shola'
String methods
.split( ) method
The result is a list of the individual words making up newString
In [122… # Examole 1
# Splitting a string
string = "hello world : how are you guys"
string.split(" ") # using space as my separator
In [124… # Examole 2
# Splitting a file name
input_file = 'account_ledger.txt'
file_name=input_file.split('.')[0] # using . as my separator
file_name
Out[124… 'account_ledger'
In [125… file_extension=input_file.split('.')[1]
file_extension
Out[125… 'txt'
Out[128… 'account_ledger.csv'
.replace( ) method
In [129… # replacing an item in a string - variable_name.replace('old_item', 'new_item')
string = "hello world helow world"
string.replace("world", "global")
In [130… string
Out[132…
.join( ) method
In [133… # Element in a list to string
myList = ["hello", "world", ":", "how", "are", "you", "gsys"]
" ".join(myList)
In [134… "-".join(myList)
Out[134… 'hello-world-:-how-are-you-gsys'
.strip( ) method
In [135… # Example 1
" Hello world ".strip()
In [136… # Example 2
"####Hello world###".strip("#")
In [137… # Example 3
"@#@#Hello world@#@#".strip("@#")
.rightstrip( ) method
In [138… " Hello world ".rstrip()
.leftstrip( ) method
In [139… " Hello world ".lstrip()
.title( ) method
In [140… "hellow world".title()
.istitle( ) method
In [142… "hellow world".istitle()
Out[142… False
.capitalize( ) method
In [143… "hellow world".capitalize()
.upper( ) method
In [144… "hellow world".upper()
.isupper( ) method
In [145… "hellow world".isupper()
Out[145… False
.lower( ) method
In [146… "HELLOW WORLD".lower()
.lower( ) method
In [147… "HELLOW WORLD".islower()
Out[147… False
"hellow world".islower()
.isalpha( ) method
In [148… "hellowworld".isalpha()
Out[148… True
In [149… "hellowworld3".isalpha()
Out[149… False
.isnumeric( ) method
In [150… "123".isnumeric()
Out[150… True
.isalnum( ) method
In [151… "123".isalnum()
Out[151… True
In [152… "hellowworld123".isalnum()
Out[152… True
.isspace() method
In [153… " ".isspace()
Out[153… True
.isidentifier() method
: is variable?
In [154… "hellow_world".isidentifier()
Out[154… True
In [155… "hellow--world".isidentifier()
Out[155… False
.zerofill( ) method
In [156… a, b, c="1","10","100"
print (a)
print (b)
print (c)
1
10
100
001
010
100
.center( ) method
In [158… "Python".center(0)
Out[158… 'Python'
In [159… "Python".center(10)
In [161… "Python".center(10,"#")
Out[161… '##Python##'
.count( ) method
In [162… "hellow world hellow world Hellow World".count("world")
Out[162… 2
Out[163… 1
.swapcase( ) method
In [164… "Python".swapcase()
Out[164… 'pYTHON'
.startswith( ) method
is starts with?
In [165… "Python".startswith("P")
Out[165… True
In [166… "Python".startswith("y")
Out[166… False
Out[167… True
.endswith( ) method
is ends with?
In [168… "Python".endswith("P")
Out[168… False
In [169… "Python".endswith("n")
Out[169… True
In [170… "Python".endswith("t",0,3)
Out[170… True
.index( ) method
In [171… # index(SubString, start, end)
"hellow world".index("w")
Out[171… 5
Out[172… 4
.find( ) method
In [174… "hellow world".find("w",0,6)
Out[174… 5
Out[175… -1
.just( ) method
Justify
In [177… "Python".rjust(10,"#")
Out[177… '####Python'
Out[178… 'Python####'
.splitlines( ) method
In [179… a=""" First line
Second line
Third line"""
a
In [180… a.splitlines()
.expandtabs( ) method
In [182… b=' First line\tSecond line\tThird line'
b.expandtabs(30)
More
Object multiplication
In [44]: x="Hello "*3
x
In [45]: x=[1,2,3]*3
x
Out[45]: [1, 2, 3, 1, 2, 3, 1, 2, 3]
In [41]: x=[[1,2,3]]*3
x
In [42]: x=(1,2,3)*3
x
Out[42]: (1, 2, 3, 1, 2, 3, 1, 2, 3)
Swap values
In [50]: width,higth=100,500
width,higth
In [51]: width,higth=higth,width
width,higth
In [52]: width,higth,z=higth,width,5
width,higth,z
In [ ]: