Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Python Programming Dr. PUSHPARANI MK, AJIET, MANGALORE1.

Similar presentations


Presentation on theme: "Introduction to Python Programming Dr. PUSHPARANI MK, AJIET, MANGALORE1."— Presentation transcript:

1 Introduction to Python Programming Dr. PUSHPARANI MK, AJIET, MANGALORE1

2 Course Title: Introduction to Python Programming Course Code: BPLCK205B Course Type: Integrated Teaching Hours/Week (L:T:P: S) : 2:0:2:0 Total Hours of Pedagogy: 40 hours CIE Marks: 50 SEE Marks: 50 Total Marks: 100 Exam Hours : 03 Credits: 03 Dr. PUSHPARANI MK, AJIET, MANGALORE2

3 Course Title: Introduction to Python Programming Course objectives 1) Learn the syntax and semantics of the Python programming language. 2) Illustrate the process of structuring the data using lists, tuples 3) Appraise the need for working with various documents like Excel, PDF, Word and Others. 4) Demonstrate the use of built-in functions to navigate the file system. 5) Implement the Object Oriented Programming concepts in Python. Dr. PUSHPARANI MK, AJIET, MANGALORE3

4 Course Title: Introduction to Python Programming Course outcome (Course Skill Set): At the end of the course the student will be able to: CO1: Demonstrate proficiency in handling loops and creation of functions. CO2: Identify the methods to create and manipulate lists, tuples and dictionaries. CO3: Develop programs for string processing and file organization CO4: Interpret the concepts of Object-Oriented Programming as used in Python. Dr. PUSHPARANI MK, AJIET, MANGALORE4

5 Course Title: Introduction to Python Programming Text Books 1.Al Sweigart,“Automate the Boring Stuff with Python”,1 stEdition, No Starch Press, 2015. (Available under CC-BY- NC-SA license at https://automatetheboringstuff.com/) (Chapters 1 to 18, except 12) for lambda functions use this link: https://www.learnbyexample.org/python- lambda-function/ 2.Allen B. Downey, “Think Python: How to Think Like a Computer Scientist”, 2 nd Edition, Green Tea Press, 2015. (Available under CC-BY-NC license at http://greenteapress.com/thinkpython2/thinkpython2. pdf (Chapters 13, 15, 16, 17, 18) (Download pdf/html files from the above link) Dr. PUSHPARANI MK, AJIET, MANGALORE5

6 Course Title: Introduction to Python Programming Module-1 (08 hrs) Python Basics: Entering Expressions into the Interactive Shell, The Integer, Floating-Point, and String Data Types, String Concatenation and Replication, Storing Values in Variables, Your First Program, Dissecting Your Program, Flow control: Boolean Values, Comparison Operators, Boolean Operators, Mixing Boolean and Comparison Operators, Elements of Flow Control, Program Execution, Flow Control Statements, Importing Modules, Ending a Program Early with sys.exit(), Functions: def Statements with Parameters, Return Values and return Statements, The None Value, Keyword Arguments and print(), Local and Global Scope, The global Statement, Exception Handling, A Short Program: Guess the Number Textbook 1: Chapters 1 – 3 Dr. PUSHPARANI MK, AJIET, MANGALORE6

7 elif Statements. In code, an elif statement always consists of the following: The elif keyword A condition (that is, an expression that evaluates to True or False) A colon Starting on the next line, an indented block of code (called the elif clause) Let’s add an elif to the name checker to see this statement in action. Dr. PUSHPARANI MK, AJIET, MANGALORE7

8 if name == 'Alice': print('Hi, Alice.') elif age < 12: print('You are not Alice, kiddo.') Dr. PUSHPARANI MK, AJIET, MANGALORE8

9 name = 'Carol' age = 3000 if name == 'Alice': print('Hi, Alice.') elif age 2000: print('Unlike you, Alice is not an undead, immortal vampire.') elif age > 100: print('You are not Alice, grannie.') Dr. PUSHPARANI MK, AJIET, MANGALORE9

10 10

11 Ex:- name = 'Carol' age = 3000 if name == 'Alice': print('Hi, Alice.') elif age 100: print('You are not Alice, grannie.') elif age > 2000: print('Unlike you, Alice is not an undead, immortal vampire.') Dr. PUSHPARANI MK, AJIET, MANGALORE11

12 Dr. PUSHPARANI MK, AJIET, MANGALORE12

13 name = 'Carol' age = 3000 if name == 'Alice': print('Hi, Alice.') elif age < 12: print('You are not Alice, kiddo.') else: print('You are neither Alice nor a little kid.') Dr. PUSHPARANI MK, AJIET, MANGALORE13

14 Dr. PUSHPARANI MK, AJIET, MANGALORE14

15 while Loop Statements In code, a while statement always consists of the following: The while keyword A condition (that is, an expression that evaluates to True or False) A colon Starting on the next line, an indented block of code (called the while clause) Dr. PUSHPARANI MK, AJIET, MANGALORE15

16 an if statement: spam = 0 if spam < 5: print('Hello, world.') spam = spam + 1 while statement: spam = 0 if spam < 5: print('Hello, world.') spam = spam + 1 Dr. PUSHPARANI MK, AJIET, MANGALORE16

17 Dr. PUSHPARANI MK, AJIET, MANGALORE17

18 An Annoying while Loop ➊ name = '' ➋ while name != 'your name': print('Please type your name.') ➌ name = input() ➍ print('Thank you!') Please type your name. Al Please type your name. Albert Please type your name. %#@#%*(^&!!! Please type your name. your name Thank you! Dr. PUSHPARANI MK, AJIET, MANGALORE18

19 Dr. PUSHPARANI MK, AJIET, MANGALORE19

20 Break Statements ➊ while True: print('Please type your name.') ➋ name = input() ➌ if name == 'your name': ➍ break ➎ print('Thank you!') Dr. PUSHPARANI MK, AJIET, MANGALORE20

21 Dr. PUSHPARANI MK, AJIET, MANGALORE21

22 Continue Statements while True: print('Who are you?') name = input() ➊ if name != 'Joe': ➋ continue print('Hello, Joe. What is the password? (It is a fish.)') ➌ password = input() if password == 'swordfish': ➍ break ➎ print('Access granted.') Dr. PUSHPARANI MK, AJIET, MANGALORE22

23 Dr. PUSHPARANI MK, AJIET, MANGALORE23

24 For loop In code, a for statement looks something like for i in range(5): and includes the following: The for keyword A variable name The in keyword A call to the range() method with up to three integers passed to it A colon Starting on the next line, an indented block of code (called the for clause) Dr. PUSHPARANI MK, AJIET, MANGALORE24

25 print('My name is') for i in range(10): print('Jimmy Five Times (' + str(i) + ')') Output:- My name is Jimmy Five Times (0) Jimmy Five Times (1) Jimmy Five Times (2) Jimmy Five Times (3) Jimmy Five Times (4) Dr. PUSHPARANI MK, AJIET, MANGALORE25

26 Dr. PUSHPARANI MK, AJIET, MANGALORE26

27 ➊ total = 0 ➋ for num in range(101): NUM=101-1=100 ➌ total = total + num………………..101+2=TOTAL….(1) ➍ print(total) The result should be 5,050. When the program first starts, the total variable is set to 0 ➊. The for loop ➋ then executes total = total + num ➌ 100 times. By the time the loop has finished all of its 100 iterations, every integer from 0 to 100 will have been added to total. At this point, total is printed to the screen ➍. Even on the slowest computers, this program takes less than a second to complete. There are 50 pairs of numbers that add up to 101: 1 + 100, 2 + 99, 3 + 98, and so on, until 50 + 51. Since 50 × 101 is 5,050, the sum of all the numbers from 0 to 100 is 5,050. Dr. PUSHPARANI MK, AJIET, MANGALORE27

28 print('My name is') i = 0 while i < 5: print('Jimmy Five Times (' + str(i) + ')') i = i + 1 Dr. PUSHPARANI MK, AJIET, MANGALORE28

29 The Starting, Stopping, and Stepping Arguments to range() for i in range(12, 20): i=12 i=20 print(i) The first argument will be where the for loop’s variable starts, and the second argument will be up to, but not including, the number to stop at. OUTPUT 12 13 14 15 16 17 18 19 Dr. PUSHPARANI MK, AJIET, MANGALORE29

30 The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration. for i in range(0, 10, 2): i=0 to i=10 0 2 4 6 8 print(i) So calling range(0, 10, 2) will count from zero to eight by intervals of two. 0 2 4 6 8 Dr. PUSHPARANI MK, AJIET, MANGALORE30

31 for i in range(5, -1, -1): print(i) This for loop would have the following output: 5 4 3 2 1 0 Running a for loop to print i with range(5, -1, -1) should print from five down to zero. Dr. PUSHPARANI MK, AJIET, MANGALORE31

32 ➊ def hello(): ➋ print('Howdy!') print('Howdy!!!') print('Hello there.') print('Howdy!') print('Howdy!!!') print('Hello there.') print('Howdy!') print('Howdy!!!') print('Hello there.') ➌ hello() hello() hello() ------------------------------------------------------------------------------ Howdy! Howdy!!! Hello there. Dr. PUSHPARANI MK, AJIET, MANGALORE32

33 ➊ def hello(name): ➋ print('Hello, ' + name) ➌ hello('Alice') hello('Bob') Hello Alice Hello Bob Dr. PUSHPARANI MK, AJIET, MANGALORE33

34 def sayHello(name): print('Hello, ' + name) ➋ sayHello('Al') Dr. PUSHPARANI MK, AJIET, MANGALORE34

35 >>> spam = print('Hello!') Hello! >>> None == spam True Dr. PUSHPARANI MK, AJIET, MANGALORE35

36 FUNCTIONS You’re already familiar with the print(), input(), and len() functions A function is like a miniprogram within a program. The first line is a def statement ➊ def hello(): ➋ print('Howdy!') print('Howdy!!!') print('Hello there.') ➌ hello() hello() hello() Howdy! Howdy!!! Hello there. Howdy! Howdy!!! Hello there. Howdy! Howdy!!! Hello there. Dr. PUSHPARANI MK, AJIET, MANGALORE36

37 DEF S TATEMENTS WITH P ARAMETERS ➊ def hello(name): ➋ print('Hello, ' + name) ➌ hello('Alice') hello('Bob') Hello, Alice Hello, Bob Dr. PUSHPARANI MK, AJIET, MANGALORE37

38 Define, Call, Pass, Argument, Parameter def sayHello(name): print('Hello, ' + name) ➋ sayHello('Al') Hello AI Dr. PUSHPARANI MK, AJIET, MANGALORE38

39 T HE N ONE V ALUE In Python, there is a value called None, which represents the absence of a value. The None value is the only value of the NoneType data type Just like the Boolean True and False values >>> spam = print('Hello!') Hello! >>> None == spam True Dr. PUSHPARANI MK, AJIET, MANGALORE39

40 K EYWORD A RGUMENTS AND THE PRINT () F UNCTION print('Hello‘, end='') print('World') Hello,World >>> print('cats', 'dogs', 'mice‘, sep=',')) cars,dogs,mice Dr. PUSHPARANI MK, AJIET, MANGALORE40

41 T HE C ALL S TACK Dr. PUSHPARANI MK, AJIET, MANGALORE41

42 Dr. PUSHPARANI MK, AJIET, MANGALORE42

43 def a(): print('a() starts') ➊ b() ➋ d() print('a() returns') def b(): print('b() starts') ➌ c() print('b() returns') def c(): ➍ print('c() starts') print('c() returns') def d(): print('d() starts') print('d() returns') ➎ a() a() starts b() starts c() starts c() returns b() returns d() starts d() returns a() returns Dr. PUSHPARANI MK, AJIET, MANGALORE43

44 L OCAL AND G LOBAL S COPE Parameters and variables that are assigned in a called function are said to exist in that function’s local scope. Variables that are assigned outside all functions are said to exist in the global scope. A variable that exists in a local scope is called a local variable, while a variable that exists in the global scope is called a global variable. A variable must be one or the other; it cannot be both local and global. Dr. PUSHPARANI MK, AJIET, MANGALORE44

45 The global statements def spam(): ➊ global eggs ➋ eggs = 'spam' eggs = 'global' spam() print(eggs) Out put spam Dr. PUSHPARANI MK, AJIET, MANGALORE45

46 def spam(): ➊ global eggs eggs = 'spam' # this is the global def bacon(): ➋ eggs = 'bacon' # this is a local def ham(): ➌ print(eggs) # this is the global eggs = 42 # this is the global spam() print(eggs) Dr. PUSHPARANI MK, AJIET, MANGALORE46

47 E XCEPTION H ANDLING def spam(divideBy): return 42 / divideBy print(spam(2)) print(spam(12)) print(spam(0)) print(spam(1)) ZeroDivisionError: division by zero Dr. PUSHPARANI MK, AJIET, MANGALORE47

48 def spam(divideBy): try: return 42 / divideBy except ZeroDivisionError: print('Error: Invalid argument.') print(spam(2)) print(spam(12)) print(spam(0)) print(spam(1)) 21.0 3.5 Error: Invalid argument. None 42.0 Dr. PUSHPARANI MK, AJIET, MANGALORE48

49 A S HORT P ROGRAM : WRITE A PROGRAM FOR Z IGZAG ******** ******** ******** ******** ******** ******** ******** ******** ******** Dr. PUSHPARANI MK, AJIET, MANGALORE49

50 try: while True: # The main program loop. print(' ' * indent, end='') print('********') time.sleep(0.1) # Pause for 1/10 of a second. if indentIncreasing: # Increase the number of spaces: indent = indent + 1 if indent == 20: # Change direction: indentIncreasing = False else: # Decrease the number of spaces: indent = indent - 1 if indent == 0: # Change direction: indentIncreasing = True except KeyboardInterrupt: sys.exit() Dr. PUSHPARANI MK, AJIET, MANGALORE50

51 Chapter-3 LIST 1)T HE L IST D ATA T YPE >>> [1, 2, 3] [1, 2, 3] >>> ['cat', 'bat', 'rat', 'elephant'] ['cat', 'bat', 'rat', 'elephant'] >>> ['hello', 3.1415, True, None, 42] ['hello', 3.1415, True, None, 42] Dr. PUSHPARANI MK, AJIET, MANGALORE51

52 1.1 Getting Individual Values in a List with Indexes ➊ >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam ['cat', 'bat', 'rat', 'elephant'] Dr. PUSHPARANI MK, AJIET, MANGALORE52

53 >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[0] 'cat' >>> spam[1] 'bat' >>> spam[2] 'rat' >>> spam[3] 'elephant‘ >>> ['cat', 'bat', 'rat', 'elephant'][3] 'elephant' ➊ >>> 'Hello, ' + spam[0] ➋ 'Hello, cat' >>> 'The ' + spam[1] + ' ate the ' + spam[0] + '.' 'The bat ate the cat.' Dr. PUSHPARANI MK, AJIET, MANGALORE53

54 >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[10000] Traceback (most recent call last): File " ", line 1, in spam[10000] IndexError: list index out of range Dr. PUSHPARANI MK, AJIET, MANGALORE54

55 >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[1] 'bat‘ >>> spam[1.0] Traceback (most recent call last): File " ", line 1, in spam[1.0] TypeError: list indices must be integers or slices, not float >>> spam[int(1.0)] 'bat' Dr. PUSHPARANI MK, AJIET, MANGALORE55

56 >>> spam = [['cat', 'bat'], [10, 20, 30, 40, 50]] >>> spam[0] ['cat', 'bat'] >>> spam[0][1] 'bat‘ >>> spam[1][4] 50 Dr. PUSHPARANI MK, AJIET, MANGALORE56

57 Negative Indexes >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[-1] 'elephant‘ >>> spam[-3] 'bat‘ >>> 'The ' + spam[-1] + ' is afraid of the ' + spam[-3] + '.' 'The elephant is afraid of the bat.' Dr. PUSHPARANI MK, AJIET, MANGALORE57

58 Getting a List from Another List with Slices spam[2] is a list with an index (one integer). spam[1:4] is a list with a slice (two integers). >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[0:4] ['cat', 'bat', 'rat', 'elephant'] >>> spam[1:3] ['bat', 'rat'] >>> spam[0:-1] ['cat', 'bat', 'rat'] Dr. PUSHPARANI MK, AJIET, MANGALORE58

59 >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[:2] ['cat', 'bat'] >>> spam[1:] ['bat', 'rat', 'elephant'] >>> spam[:] ['cat', 'bat', 'rat', 'elephant'] Dr. PUSHPARANI MK, AJIET, MANGALORE59

60 Getting a List’s Length with the len() Function >>> spam = ['cat', 'dog', 'moose'] >>> len(spam) 3 Dr. PUSHPARANI MK, AJIET, MANGALORE60

61 Changing Values in a List with Indexes >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> spam[1] = 'aardvark‘ >>> spam ['cat', 'aardvark', 'rat', 'elephant'] >>> spam[2] = spam[1] >>> spam ['cat', 'aardvark', 'aardvark', 'elephant'] >>> spam[-1] = 12345 >>> spam ['cat', 'aardvark', 'aardvark', 12345] Dr. PUSHPARANI MK, AJIET, MANGALORE61

62 List Concatenation and List Replication >>> [1, 2, 3] + ['A', 'B', 'C'] [1, 2, 3, 'A', 'B', 'C'] >>> ['X', 'Y', 'Z'] * 3 ['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z'] >>> spam = [1, 2, 3] >>> spam = spam + ['A', 'B', 'C'] >>> spam [1, 2, 3, 'A', 'B', 'C'] Dr. PUSHPARANI MK, AJIET, MANGALORE62

63 Removing Values from Lists with del Statements >>> spam = ['cat', 'bat', 'rat', 'elephant'] >>> del spam[2] >>> spam ['cat', 'bat', 'elephant'] >>> del spam[2] >>> spam ['cat', 'bat'] Dr. PUSHPARANI MK, AJIET, MANGALORE63

64 1 ) List data type 1.1 Getting Individual Values in a List with Indexes 1.2 Negative Indexes 1.3 Getting a List from Another List with Slices 1.4 Getting a List’s Length with the len() Function 1.5 Changing Values in a List with Indexes 1.6 List Concatenation and List Replication 1.7 Removing Values from Lists with del Statements Dr. PUSHPARANI MK, AJIET, MANGALORE64

65 2) W ORKING WITH L ISTS catName1 = 'Zophie' catName2 = 'Pooka' catName3 = 'Simon' catName4 = 'Lady Macbeth' catName5 = 'Fat-tail' catName6 = 'Miss Cleo' Dr. PUSHPARANI MK, AJIET, MANGALORE65

66 print('Enter the name of cat 1:') catName1 = input() print('Enter the name of cat 2:') catName2 = input() print('Enter the name of cat 3:') catName3 = input() print('Enter the name of cat 4:') catName4 = input() print('Enter the name of cat 5:') catName5 = input() print('Enter the name of cat 6:') catName6 = input() print('The cat names are:') print(catName1 + ' ' + catName2 + ' ' + catName3 + ' ' + catName4 + ' ' + catName5 + ' ' + catName6) Dr. PUSHPARANI MK, AJIET, MANGALORE66

67 catNames = [] while True: print('Enter the name of cat ' + str(len(catNames) + 1) + ' (Or enter nothing to stop.):') name = input() if name == '': break catNames = catNames + [name] # list concatenation print('The cat names are:') for name in catNames: print(' ' + name) Dr. PUSHPARANI MK, AJIET, MANGALORE67

68 7. References >>> spam = 42 >>> cheese = spam >>> spam = 100 >>> spam 100 >>> cheese 42 Dr. PUSHPARANI MK, AJIET, MANGALORE68

69 ➊ >>> spam = [0, 1, 2, 3, 4, 5] ➋ >>> cheese = spam # The reference is being copied, not the list. ➌ >>> cheese[1] = 'Hello!' # This changes the list value. >>> spam [0, 'Hello!', 2, 3, 4, 5] >>> cheese # The cheese variable refers to the same list. [0, 'Hello!', 2, 3, 4, 5] Dr. PUSHPARANI MK, AJIET, MANGALORE69

70 Dr. PUSHPARANI MK, AJIET, MANGALORE70


Download ppt "Introduction to Python Programming Dr. PUSHPARANI MK, AJIET, MANGALORE1."

Similar presentations


Ads by Google