Documente Academic
Documente Profesional
Documente Cultură
ArtificiAl
intelligence
with python
file:///C:/Users/Zayan/Desktop/PyLecture2.html 1/6
01/03/2020 PyLecture2
Lecture No 1
Python Programming
Programming Fundamentals
Object Oriented Programming
Data Structures
1 - python.org
2 - vs code
Windows-cmd
In [1]:
(AMD64)] on win32
In [2]:
Hello, World!
Happy Learning
Lecture No 2
Python Interpreter
In [1]:
In [2]:
Hello, World!
'Hello, World!'
print ('Hello, World!')
We are using the 'print' statement
Text strings (str) are always put in 'single' or "double" quotes
Python interpreter can be used as a calculator
Try the following one by one:
5 + 2, 5 - 2, 5 * 2, 5 ** 2, 5 / 2, 5 % 2, 5.0 / 2, 5 / 2.0, 5.0 / 2.0
+, -, * etc. are operators
5 and 2 are operands
file:///C:/Users/Zayan/Desktop/PyLecture2.html 3/6
01/03/2020 PyLecture2
In [3]:
Hello, World!
In [4]:
Docstring:
Hello, World!
In [6]: 5 + 2
Out[6]: 7
In [7]: 5 - 2
Out[7]: 3
In [8]: 5 * 2
Out[8]: 10
In [9]: 5 ** 2
Out[9]: 25
In [10]: 5 / 2
Out[10]: 2.5
In [11]: 5 % 2
Out[11]: 1
In [12]: 5.0 / 2
Out[12]: 2.5
file:///C:/Users/Zayan/Desktop/PyLecture2.html 4/6
01/03/2020 PyLecture2
In [13]: 5 / 2.0
Out[13]: 2.5
Out[14]: 2.5
In [15]: 5 // 2
Out[15]: 2
In [16]:
Out[16]: str
In [17]:
Out[17]: float
In [18]:
Out[18]: int
In [19]:
Out[19]: int
In [20]:
Out[20]: float
In [21]:
Out[21]: str
file:///C:/Users/Zayan/Desktop/PyLecture2.html 3/6
01/03/2020 PyLecture2
In [22]:
Out[22]:
bool
Variables
In [23]: x = 2
In [24]: print(x)
In [25]: type(x)
Out[25]: Int
In [26]:
In [27]:
Hello, World!
In [28]:
Out[28]: str
In [29]:
In [30]:
In [31]:
In [32]:
In [33]:
Out[33]: str
file:///C:/Users/Zayan/Desktop/PyLecture2.html 4/6
01/03/2020 PyLecture2
In [35]: print(value)
10
In [36]: type(value)
Out[36]: Int
In [37]:
In [38]:
10.0
In [39]:
Out[39]: float
In [40]:
Out[40]: 12
In [41]:
12
VS Code
Link: https://code.visualstudio.com/ (https://code.visualstudio.com/)
Python extension for Visual Studio Code: https://marketplace.visualstudio.com/items?itemName=ms-
python.python (https://marketplace.visualstudio.com/items?itemName=ms-python.python)
file:///C:/Users/Zayan/Desktop/PyLecture2.html 5/6
01/03/2020 PyLecture3
https://code.visualstudio.com/docs/python/python-tutorial (https://code.visualstudio.com/docs/python/python-
tutorial)
Open VS Code
Close Welcome Screen
Click Explorer (Ctrl + Shift + E)
Open Folder
Click New File with .py
Example: main.py
Write Hello World Python Program
Ctrl + S
Ctrl + `
python main.py
Enter
Note:
Install Python Extension
Ctrl + Shift + P - Select Interpreter
Lecture No 3
Print Function
In [1]: # help(print)
In [2]: print?
Docstring:
print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
In [3]:
In [4]:
In [5]: print(end='')
Hello,
World!
Hello, World!
In [8]:
45
(6-9j)
True
245.8
Python
Programming
20 True
ML NLP
<class 'str'>
In [9]:
AI AP
In [10]:
AIAP
In [11]:
AI - AP
file:///C:/Users/Zayan/Desktop/PyLecture3.html 7/17
01/03/2020 PyLecture3
In [12]:
AI & AP
In [13]: x = 35
In [14]:
35
25.8
SIn [15]:
35 25.8
In [16]:
In [17]:
5 10 20.9
In [18]:
print(5 + 10)
print(8 / 5)
print(8 // 5)
print(3 ** 4)
5
15
1.6
1
81
Integer
In [19]: v1 = 10
In [20]:
file:///C:/Users/Zayan/Desktop/PyLecture3.html 8/17
01/03/2020 PyLecture3
In [21]: print(v3)
25
In [22]: v4 = int('34')
In [23]: print(v4)
34
In [24]: v5 = int(True)
In [25]: print(v5)
In [26]: v6 = int(False)
In [27]: print(v6)
In [28]:
Complex
In [29]:
In [30]:
(4+8j)
file:///C:/Users/Zayan/Desktop/PyLecture3.html 9/17
01/03/2020 PyLecture3
In [31]:
In [32]:
Create a complex number from a real part and an optional imaginary part.
Subclasses:
Boolean
In [33]: b1 = True
b2 = False
In [34]:
In [35]:
In [36]: print(b4)
True
In [37]: bool?
Subclasses:
Float
In [38]:
In [39]:
In [40]:
In [41]: print(f3)
47.0
In [42]: f4 = float('37.9')
file:///C:/Users/Zayan/Desktop/PyLecture3.html 10
01/03/2020 PyLecture3
In [43]: print(f4)
37.9
In [44]: float?
String Manipulation
Display String
python programming
python programming
python programming
python programming
file:///C:/Users/Zayan/Desktop/PyLecture3.html 11
01/03/2020 PyLecture3
String Variables
In [53]:
In [54]:
In [55]:
In [56]:
In [57]:
In [58]:
In [59]:
In [60]:
In [61]:
In [62]: s9 = str('101')
In [63]: print(type(s9))
<class 'str'>
In [64]: print(s9)
101
In [65]:
String Operations
In [66]:
In [67]:
In [68]:
Out[68]: 'helloworld'
In [69]:
In [70]:
Out[70]: 'hellohellohello'
In [71]:
file:///C:/Users/Zayan/Desktop/PyLecture3.html 13
01/03/2020 PyLecture3
In [72]:
In [73]:
Out[73]: 5
The in Operator
Try the following:
'e' in 'hello'
'a' in 'hello'
Out[74]: True
Out[75]: False
In [76]: m1 =
In [77]:
Out[77]: 'h'
In [78]:
Out[78]:
'he'
In [79]: m3 =
In [80]:
Out[80]: 'ceg'
In [81]:
In [82]:
file:///C:/Users/Zayan/Desktop/PyLecture3.html 14
01/03/2020 PyLecture3
In [83]:
Out[83]: 'p'
In [84]:
Out[84]: 'n'
In [85]:
Out[85]: 'p'
In [86]:
Out[86]: 'n'
In [87]:
In [88]:
Out[88]: 18
In [89]:
In [90]:
In [91]: course[2:len(course)]
In [92]: course[2:19]
In [93]: course[:18]
In [94]: course[:len(course)]
In [95]: course[2:5]
Out[95]: 'tho'
In [96]: course[:]
file:///C:/Users/Zayan/Desktop/PyLecture3.html 15
01/03/2020 PyLecture3
In [99]: course[ : : ]
In [101]: course[:-2]
file:///C:/Users/Zayan/Desktop/PyLecture3.html 16
01/03/2020 PyLecture3
In [103]: print(type(a))
<class 'float'>
In [104]: b = int(a)
In [105]: print(type(b))
<class 'int'>
In [106]: print(b)
10
In [107]: n1 = int(45.6)
In [108]: print(n1)
45
In [109]: # n2 = int('45.6') # invalid literal for int() with base 10: '45.6'
In [110]: n2 = int('45')
In [111]: print(n2)
45
In [112]: print(type(n2))
<class 'int'>
In [113]: n3 = float(45)
In [114]: print(n3)
45.0
In [115]: n4 = float('35.6')
In [116]: print(n4)
35.6
In [117]: n5 = str(45)
file:///C:/Users/Zayan/Desktop/PyLecture3.html 13
01/03/2020 PyLecture3
In [118]: n6 = str(45.7)
In [119]: print(n6)
45.7
In [120]: n7 = str(True)
In [121]: print(n7)
True
In [122]: s = 'string'
In [123]: print(dir(s))
[' add ', ' class ', ' contains ', ' delattr ', ' dir ', ' doc ',
' eq ', ' format ', ' ge ', ' getattribute ', ' getitem ', ' getne
wargs ', ' gt ', ' hash ', ' init ', ' init_subclass ', ' iter ',
' le ', ' len ', ' lt ', ' mod ', ' mul ', ' ne ', ' new ', '
reduce ', ' reduce_ex ', ' repr ', ' rmod ', ' rmul ', ' setattr_
_', ' sizeof ', ' str ', ' subclasshook ', 'capitalize', 'casefold', 'c
enter', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'forma
t_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'is
identifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'is
upper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replac
e', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 's
plitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
file:///C:/Users/Zayan/Desktop/PyLecture3.html 14
01/03/2020 PyLecture3
String 45 Methods
capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index',
'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle',
'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit',
'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'
In [124]: # s.
In [125]: # s.<tab_press>
In [126]: # str.<tab_press>
In [127]: str.upper?
Signature: str.upper(self, /)
The string upper() method converts all lowercase characters in a string into uppercase characters and returns it.
The upper() method returns the uppercased string from the given string. It converts all lowecase characters to
uppercase.
In [130]: s1.upper()
In [131]: s1
file:///C:/Users/Zayan/Desktop/PyLecture3.html 15
01/03/2020 PyLecture3
s2
In [132]:
In [133]: s2 = s1.upper()
Out[133]: 'PYTHON PROGRAMMING'
In [134]: s1
In [135]: s1 == s2
Out[135]: False
Out[136]: True
Python Assignments
Assignment 1 - PEP 8 (Style Guide for Python Code - Python.org)
Assignment 2 Hint
file:///C:/Users/Zayan/Desktop/PyLecture3.html 16
01/03/2020 PyLecture4
Lecture no 4
In [1]:
In [2]:
[' add ', ' class ', ' contains ', ' delattr ', ' dir ', ' doc ',
' eq ', ' format ', ' ge ', ' getattribute ', ' getitem ', ' getne
wargs ', ' gt ', ' hash ', ' init ', ' init_subclass ', ' iter ',
' le ', ' len ', ' lt ', ' mod ', ' mul ', ' ne ', ' new ', '
reduce ', ' reduce_ex ', ' repr ', ' rmod ', ' rmul ', ' setattr_
_', ' sizeof ', ' str ', ' subclasshook ', 'capitalize', 'casefold', 'c
enter', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'forma
t_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'is
identifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'is
upper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replac
e', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 's
plitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper',
'zfill']
Docstring:
Return -1 on failure.
Type: method_descriptor
The find() method returns the index of first occurrence of the substring (if found). If not found, it returns -1.
file:///C:/Users/Zayan/Desktop/PyLecture4.html 1/27
01/03/2020 PyLecture4
In [5]: print(s1.find('gmail'))
15
In [6]: print(s1.find('com'))
21
In [7]: print(s1.find('outlook'))
-1
True
-1
file:///C:/Users/Zayan/Desktop/PyLecture4.html 2/27
01/03/2020 PyLecture4
In [10]:
Out[10]:
'rizwan.datahub@gmail.com'
In [11]:
14
In [12]:
14
In [13]:
-1
In [14]:
14
In [15]:
-1
In [17]:
gmail.com
Docstring:
file:///C:/Users/Zayan/Desktop/PyLecture4.html 3/27
01/03/2020 PyLecture4
The string format() method formats the given string into a nicer output in Python.
Link1: https://docs.python.org/3/library/stdtypes.html#str.format
(https://docs.python.org/3/library/stdtypes.html#str.format)
Link2: https://docs.python.org/3/library/string.html#formatstrings
(https://docs.python.org/3/library/string.html#formatstrings)
Here, pos0, pos1,... are positional arguments and, key0, key1,... are keyword arguments with values
val0, val1,... respectively.
And, template_string is a mixture of format codes with placeholders for the arguments.
In [19]:
The string "Hi {0}, your current amount is {1:9.2f}" is the template string. This contains the format
codes for formatting.
file:///C:/Users/Zayan/Desktop/PyLecture4.html 13
01/03/2020 PyLecture4
The curly braces are just placeholders for the arguments to be placed. In the above example, {0} is
placeholder for "Rizwan" and {1:9.2f} is placeholder for 50000.989 .
Since the template string references format() arguments as {0} and {1}, the arguments are positional arguments.
They both can also be referenced without the numbers as {} and Python internally converts them to numbers.
Internally,
Since "Rizwan" is the 0th argument, it is placed in place of {0}. Since, {0} doesn't contain any other format
codes, it doesn't perform any other operations.
However, it is not the case for 1st argument 50000.989 . Here, {1:9.2f} places 50000.989 in its place
and performs the operation 9.2f.
f specifies the format is dealing with a float number. If not correctly specified, it will give out an error.
The part before the "." (9) specifies the minimum width/padding the number (50000.989) can take. In this
case, 50000.989 is allotted a minimum of 9 places including the ".".
If no alignment option is specified, it is aligned to the right of the remaining spaces. (For strings, it is aligned
to the left.)
The part after the "." (2) truncates the decimal part (989) upto the given number. In this case, 989 is
truncated after 2 places.
Remaining numbers (9) is rounded off outputting 99.
In [20]:
I've used the same example from above to show the difference between keyword and positional arguments.
Here, instead of just the parameters, we've used a key-value for the parameters. Namely, name="Rizwan" and
amt=50000.989 .
Since, these parameters are referenced by their keys as {name} and {amt:9.2f} , they are known as
keyword or named arguments.
Internally,
The placeholder {name} is replaced by the value of name - "Rizwan". Since, it doesn't
contain any other format codes, "Rizwan" is placed.
For the argument amt=50000.989, the placeholder {amt:9.2f} is replaced by the value
50000.989. But before replacing it, like previous example, it performs 9.2f operation
on it.
This outputs 50000.99. The decimal part is truncated after 2 places and remaining
digits are rounded off. Likewise, the total width is assigned 9 leaving one spaces to
the left.
file:///C:/Users/Zayan/Desktop/PyLecture4.html 14
01/03/2020 PyLecture4
In [21]:
In [22]:
In [23]:
In [24]:
Note: In case of mixed arguments, keyword arguments has to always follow positional arguments.
You can format numbers using the format specifier given below:
Type - Meaning
d-Decimal integer
c-Corresponding Unicode character
b-Binary format
o-Octal format
x-Hexadecimal format (lower case)
X-Hexadecimal format (upper case)
n-Same as 'd'. Except it uses current locale setting for number separator
e-Exponential notation. (lowercase e)
E-Exponential notation (uppercase E)
- Displays fixed point number (Default: 6)
file:///C:/Users/Zayan/Desktop/PyLecture4.html 15
01/03/2020 PyLecture4
In [25]:
In [26]:
# float arguments
In [27]:
# octal, binary and hexadecimal format
In [28]:
# integer numbers with minimum width
print("{0:5d}".format(12))
Out [28]: 12
In [29]:
# width doesn't work for numbers longer than padding
print("{:2d}".format(1234))
file:///C:/Users/Zayan/Desktop/PyLecture4.html 16
01/03/2020 PyLecture4
In [30]:
# padding for float numbers
print("{:8.3f}".format(12.2346))
print("{:05d}".format(12))
In [32]:
# padding for float numbers filled with zeros
print("{:08.3f}".format(12.2346))
Here,
In the first statement, {0:5d} takes an integer argument and assigns a minimum width of 5. Since, no alignment is
specified, it is aligned to the right.
In the second statement, you can see the width (2) is less than the number (1234), so it doesn't take
any space to the left but also doesn't truncate the number.
Unlike integers, floats has both integer and decimal parts. And, the mininum width defined to the number
is for both parts as a whole including ".".
In the third statement, {:8.3f} truncates the decimal part into 3 places rounding off the last 2 digits. And,
the number, now 12.235, takes a width of 8 as a whole leaving 2 places to the left.
If you want to fill the remaining places with zero, placing a zero before the format specifier does this. It
works both for integers and floats: {:05d} and {:08.3f}.
In [33]:
# integer numbers with right alignment
print("{:5d}".format(12))
Out [33]: 12
file:///C:/Users/Zayan/Desktop/PyLecture4.html 17
01/03/2020 PyLecture4
In [34]:
# float numbers with center alignment
print("{:*^10.3f}".format(12.2346))
In [35]:
# string padding with left alignment
print("{:8}".format("python"))
In [36]:
# string padding with right alignment
print("{:>8}".format("python"))
In [37]:
# string padding with center alignment
print("{:^8}".format("python"))
In [38]:
# string padding with center alignment #
and '*' padding character
print("{:*^8}".format("python"))
file:///C:/Users/Zayan/Desktop/PyLecture4.html 18
01/03/2020 PyLecture4
cgpa = 3.9
In [40]:
name + str(cgpa)
In [41]:
name + " " + str(cgpa)
In [42]:
print('My Info - Name : ' + name + ', CGPA : ' + str(cgpa))
In [44]:
print('My Info - Name : {1}, CGPA : {0}'.format(cgpa, name))
file:///C:/Users/Zayan/Desktop/PyLecture4.html 19
01/03/2020 PyLecture4
In [45]:
print('My Info - Name : {name}, CGPA : {cgpa}'.format(cgpa=3.9, name='ahmad'))
In [46]:
print(f'My Info - Name : {name}, CGPA : {cgpa}')
(f-string)
file:///C:/Users/Zayan/Desktop/PyLecture4.html 20
01/03/2020 PyLecture4
file:///C:/Users/Zayan/Desktop/PyLecture4.html 21