Introduction to Python Programming

Introduction to Python Programming

Overview......................................................................................................................................... 1 What You Will Learn................................................................................................................ 2

First Program: Hello World ............................................................................................................ 3 Writing a Program..................................................................................................................... 3 Python Interpreter ..................................................................................................................... 3

Variables ......................................................................................................................................... 4 Assigning Literal Values to Variables ...................................................................................... 4 Working with String Variables ................................................................................................. 9 User Inputs .............................................................................................................................. 10

Exercise 1...................................................................................................................................... 11

Conditionals .................................................................................................................................. 12 Writing Simple If Statements.................................................................................................. 12 Using Elif for Multiple Conditionals ...................................................................................... 15 Working with Logical Operators ............................................................................................ 17 Understanding Nested If Statements....................................................................................... 18 Putting It All Together ............................................................................................................ 19

Exercise 2...................................................................................................................................... 21

Working with While Loops .......................................................................................................... 22

Generating a Random Number ..................................................................................................... 25

Exercise 3...................................................................................................................................... 26

Using For Loops ........................................................................................................................... 27 Controlling the Flow of Loops................................................................................................ 29 Nesting Loops ......................................................................................................................... 29 Other For Loop Options.......................................................................................................... 30

Exercise 4...................................................................................................................................... 32

Built-In Functions and Methods ................................................................................................... 33 Built-in Functions ................................................................................................................... 33 String Manipulation and the Slicer Operator (Substrings) ..................................................... 34

Exercise 5...................................................................................................................................... 36

Data Structures.............................................................................................................................. 37 Lists......................................................................................................................................... 37 Updating List Values .............................................................................................................. 38 Appending a Value to a List ................................................................................................... 38

UMSL Computer Education and Training Center cetc.umsl.edu

Using Indexes.......................................................................................................................... 38 Removing a Value from a List................................................................................................ 38 Functions on a List.................................................................................................................. 39 Looping through a List............................................................................................................ 39 Sorting a List........................................................................................................................... 40 Complex Lists ......................................................................................................................... 40

Exercise 6...................................................................................................................................... 41

Tuples............................................................................................................................................ 42

Dictionaries ................................................................................................................................... 44 Defining and Initializing a Dictionary .................................................................................... 44 Updating and Adding Entries to a Dictionary......................................................................... 44 Finding an Entry in a Dictionary ............................................................................................ 45 Showing Dictionary Values .................................................................................................... 45 Updating/Merging Dictionary................................................................................................. 46 Deleting Entries from a Dictionary......................................................................................... 46

Exercise 7...................................................................................................................................... 47

Functions....................................................................................................................................... 48 Defining Your Own Functions................................................................................................ 48 Parameters............................................................................................................................... 49

Exercise 8...................................................................................................................................... 50

Modules......................................................................................................................................... 51

Exercise 9...................................................................................................................................... 52

More on Functions ........................................................................................................................ 53 Functions: "First-Class Citizens" ............................................................................................ 53 Passing Functions to a Function ............................................................................................. 53 Functions in a Dictionary........................................................................................................ 54 Functions Inside of Functions ................................................................................................. 54

Appendix A: Notepad++ Hot Key ................................................................................................ 56

Appendix B: Codes Used in the Strftime Function ...................................................................... 57

Appendix C: Standard Modules.................................................................................................... 59

Appendix D: Solutions to Exercises ............................................................................................. 60

UMSL Computer Education and Training Center cetc.umsl.edu

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download