0% found this document useful (0 votes)
7 views4 pages

AI Lec1

.....

Uploaded by

pierresalam6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

AI Lec1

.....

Uploaded by

pierresalam6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Introduction To Artificial Intelligence Course

First Lecture - Introduction To Python

> Eng. Manal Farhoudah , Eng. Loujain AboKaff

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.

Installing the Anaconda distribution.


In this course we will work with The Jupyter Notebook application which allows us to create and edit
documents that display the input and output of a Python language script.

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

pip install some_Package

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

Note : random is used for generating random numbers.

In [2]:
help(randint)

Help on method randint in module random:

randint(a, b) method of random.Random instance


randint(a, b) method of random.Random instance
Return random integer in range [a, b], including both end points.

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)

My name is John, I am male

In [8]:
print(txt[2:5])

na

Python Data Structures


Python has implicit support for Data Structures which enable you to store and access data. Some of These
structures are:

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]:

my_list = [1, 2, 3, 'example', 3.132]


print(my_list)

[1, 2, 3, 'example', 3.132]

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

traverse lists using for loop:

In [12]:

my_list = [1, 2, 3, 'example', 3.132, 10, 30]


for element in my_list: #access elements one by one
print(element)

1
2
3
example
3.132
10
30

access elements using indexes:

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= []

for ele in inputList:


outputList.append(ele*ele)

outputList= outputList[:: -1]

outputList
Out[16]:
[100, 64, 36, 16, 4]

You might also like