Top Banner
CSC462: Introduction to Artificial Intelligence (Lab) Department of Computer Sciences COMSATS Institute of Information Technology Abbottabad Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab) 1 / 19
20

introduction to python

Feb 22, 2017

Download

Technology

Sardar Alam
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: introduction to python

CSC462: Introduction to ArtificialIntelligence (Lab)

Department of Computer SciencesCOMSATS Institute of Information Technology

Abbottabad

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)1 / 19

Page 2: introduction to python

Introduction Python

Dr. Zia ur Rehman

COMSATS Institute of Information TechnologyAbbottabad, Pakistan

Fall 2016

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)2 / 19

Page 3: introduction to python

What is Python?

Python was created by a Dutchman named Guido van Rossumin the early 90s.It is now one of the most popular languages in existence.There are two versions of Python:

Python 2.7Python 3.5

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)3 / 19

Page 4: introduction to python

Installing Python

There are several ways for installing Python and its associatedtools.The easiest is to donwload and install the anacondadistribution from:https://www.continuum.io/downloads

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)4 / 19

Page 5: introduction to python

Which setup to download?

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)5 / 19

Page 6: introduction to python

Spyder IDE

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)6 / 19

Page 7: introduction to python

Variables and Assignment

1 x = 3 # Create variable x and assign value 3 to it

2 x = x*x # Bind x to value 9

3 print(x)

Click on the variable explorer tab (below top right window) inSpyder IDE to see the variable type at the value assigned.Code can be type in the script window (left) or the pythonconsole (bottom right).

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)7 / 19

Page 8: introduction to python

Variable Types

All variables in Python are objects!The type() function returns the variable (or literal) type.

1 y = input(’enter a number:’)

2 print (type(y))

3 print (y)

1 y = float(input(’Enter a number: ’))

2 print (type(y))

3 print (y)

4 print (y*y)

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)8 / 19

Page 9: introduction to python

Variables in C/C++/Java vs Variables in Python

1 int x;

2 int y;

3

4

5 x=42;

6 y=42;

7

8

9 y=78;

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)9 / 19

Page 10: introduction to python

Variables in Python

There is no declaration of variables required in Python. It’s noteven possible. If there is need of a variable, you think of aname and start using it as a variable.Not only the value of a variable may change during programexecution but the type as well. You can assign an integer valueto a variable, use it as an integer for a while and then assign astring to the same variable.

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)10 / 19

Page 11: introduction to python

Variables in Python

1 i = 42 # assgnment

2 i = i + 1 # addition operatior

3 print(i) # print the result

1 i = 42 # data type is implicitly set to

integer↪→

2 i = 42 + 0.11 # data type is changed to float

3 i = "fourty" # and now it will be a string

Python variables are references to objects, but the actual data iscontained in the objects

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)11 / 19

Page 12: introduction to python

Variables in Python

1 x=42

2

3

4

5

6 y=x

7

8

9

10

11 y=78

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)12 / 19

Page 13: introduction to python

Variables in Python

1 x = "Text" # Now x

references a string↪→

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)13 / 19

Page 14: introduction to python

id Function

How can we see or provethat x and y reallyreference the same objectafter the assignment y = xthe previous example?The identity function id()can be used for thispurpose.Every instance (object orvariable) has an identity,i.e. an integer which isunique within the script orprogram, i.e. other objectshave different identities.

Type the following code in thepython console window.

1 >>> x = 42

2 >>> id(x)

3 10107136

4 >>> y = x

5 >>> id(x), id(y)

6 (10107136, 10107136)

7 >>> y = 78

8 >>> id(x), id(y)

9 (10107136, 10108288)

10 >>>

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)14 / 19

Page 15: introduction to python

Blocks and Indentation

Other languages use { } or Begin: End: kewords to markcode blocks. Python uses a different principle.Python programs get structured through indentation, i.e. codeblocks are defined by their indentation.Code indentation is used in other languages as well but in caseof Python it’s a language requirement not a matter of style.Loops and Conditional statements end with a colon ":" - thesame is true for functions and other structures introducingblocks.

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)15 / 19

Page 16: introduction to python

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)16 / 19

Page 17: introduction to python

Indentation example

1 x = int(input(’Enter an integer: ’))

2 if x%2 == 0:

3 print(’Even’)

4 else:

5 print(’Odd’)

6 if x%3 != 0:

7 print (’And not divisible by 3’)

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)17 / 19

Page 18: introduction to python

Variable Names

The naming of variables follows the more general concept ofan identifier.A Python identifier is a name used to identify a variable,function, class, module or other object.A variable name and an identifier can consist of the uppercaseletters "A" through "Z", the lowercase letters "a" through"z", the underscore _ and, except for the first character, thedigits 0 through 9.Python 3.x is based on Unicode. This means that variablenames and identifier names can additionally contain Unicodecharacters as well.Identifiers are unlimited in length. Case is significant.Exceptions from the rules above are the special Pythonkeywords

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)18 / 19

Page 19: introduction to python

Python Keywords I

No identifier can have the same name as one of the Pythonkeywords, although they are obeying the above naming conventions:

1 and, as, assert, break, class, continue, def, del,

elif, else,↪→

2 except, False, finally, for, from, global, if, import,

in, is,↪→

3 lambda, None, nonlocal, not, or, pass, raise, return,

True, try,↪→

4 while, with, yield

There is no need to learn them by heart. You can get the list ofPython keywords in the interactive shell by using help. You type inhelp() in the interactive console:

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)19 / 19

Page 20: introduction to python

Python Keywords II

1 help()

2 help>

This will show he help prompt. Now type the following statement:

1 help> keywords

Dr. Zia ur Rehman CSC462: Introduction to Artificial Intelligence (Lab)20 / 19