AI Lec1
AI Lec1
Session Outlines
Why Python?
Pyhon Libraries.
Python Variables
Python Data Structures.
Exercises.
Why Python?
Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-
level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for
Rapid Application Development, as well as for use as a scripting or glue language to connect existing
components together.
We will use the Anaconda distribution to install Jupyter. you can go through Jupyter installation in this link.
Python has a lot of libraries and moudules, you can easily install packages using pip statement from
command prompt windows
Package importing
We should import packages befor we use it using import keyword
In [1]:
from random import randint # Package
print(randint(1, 200))
27
In [2]:
help(randint)
Python Variables
Python is a dynamically typed language, so you do not define the type of variable you are creating
In [3]:
f = 5.5
s ="5"
In [4]:
type(f)
Out[4]:
float
In [5]:
type(s)
Out[5]:
str
In [6]:
int ("5") * 5
Out[6]:
25
In [7]:
gender = "male"
txt = "My name is John, I am " + gender
print(txt)
In [8]:
print(txt[2:5])
na
List
Dictionary
Tuple
Set
Lists
Lists are a mutable data structure which used to store data of different data types in a sequential manner.
In [9]:
my_list = []
my_list = []
print(my_list)
[]
In [10]:
Adding Elements
Adding elements in the list can be achieved using the append(), extend() and insert() functions.
The append() function adds all the elements passed to it as a single element.
The extend() function adds the elements one-by-one into the list.
The insert() function adds the element passed to the index value and increase the size of the list too.
In [11]:
my_list = [1, 2, 3]
print(my_list)
my_list.append([555, 12]) #add as a single element
print(my_list)
my_list.extend([234, 'more_example']) #add as different elements
print(my_list)
my_list.insert(1, 'insert_example') #add element i
print(my_list)
[1, 2, 3]
[1, 2, 3, [555, 12]]
[1, 2, 3, [555, 12], 234, 'more_example']
[1, 'insert_example', 2, 3, [555, 12], 234, 'more_example']
Accessing Elements
In [12]:
1
2
3
example
3.132
10
30
In [13]:
print(my_list[3]) #access index 3 element
print(my_list[0:2]) #access elements from 0 to 1 and exclude 2
print(my_list[0::2])
print(my_list[::-1])
example
[1, 2]
[1, 3, 3.132, 30]
[30, 10, 3.132, 'example', 3, 2, 1]
In [14]:
In [14]:
my_list.remove("example")
my_list
Out[14]:
[1, 2, 3, 3.132, 10, 30]
In [15]:
my_list.pop(2)
my_list
Out[15]:
[1, 2, 3.132, 10, 30]
List Exercise
In [16]:
inputList= [2,4,6,8,10]
outputList= []
outputList
Out[16]:
[100, 64, 36, 16, 4]