Python – Part 1 Python Programming Language 1
What is Python? High-level language Interpreted – easy to test and use interactively Object-oriented Open-source software 2 Prepared by Department of Preparatory year
What is Python? Small, simple language Extensible – set of modules as tools to build on the language Familiar – features from other languages (Java, C, Perl) 3 Prepared by Department of Preparatory year
Benefits Consistency – maintained by a large organization of people Scalability – Python projects scale to very large projects Flexibility – use of libraries and programs written in other languages 4 Prepared by Department of Preparatory year
Benefits Interactive interpreter Cross-platform Easy to implement 5 Prepared by Department of Preparatory year
Resources Python: Installing Python: Python Documentation: Prepared by Department of Preparatory year
Resources Python.org's list of editors and IDEseditorsIDEs – – entEnvironments entEnvironments Think Python – kpython.html kpython.html 7 Prepared by Department of Preparatory year
Python Programs Executed by an interpreter Two ways to use interpreter – Interactive mode – Script mode 8 Prepared by Department of Preparatory year
Installing Python Python 3 is used in this course phEA phEA python-windows.html python-windows.html Prepared by Department of Preparatory year 9
InteractiveMode >>> >>> is the prompt the interpreter uses to indicate that it is ready. If you type 1 + 1, the interpreter replies 2. Prepared by Department of Preparatory year 10
Script Mode Store code in a file Use the interpreter to execute the contents of the file (called script,.py extention) 11 Prepared by Department of Preparatory year
Programs A program is a sequence of instructions that specifies how to perform a computation. – input: Get data from the keyboard, a file, or some other device. – output: Display data on the screen or send data to a file or other device. 12 Prepared by Department of Preparatory year
Programs (cont…) – math: Perform basic mathematical operations like addition and multiplication. – conditional execution: Check for certain conditions and execute the appropriate sequence of statements. – repetition: Perform some action repeatedly, usually with some variation. 13 Prepared by Department of Preparatory year
Debugging Programming errors are called bugs Debugging - the process of tracking them down. Three kinds of errors: – syntax errors – runtime errors – semantic errors 14 Prepared by Department of Preparatory year
Syntax errors Occur if syntax is not correct Syntax refers to the structure of a program and the rules about that structure. (1 + 2) is legal 8) is a syntax error 15 Prepared by Department of Preparatory year
Runtime errors Error does not appear until after the program has started running Also called exceptions Rare in simple programs 16 Prepared by Department of Preparatory year
Semantic errors Program runs successfully (no error messages generated) Will not do the right thing The meaning of the program (its semantics) is wrong. Tricky to identify 17 Prepared by Department of Preparatory year
The first program Hello World program – print (‘Hello, World!’) Print statement Display on the screen: – Hello World 18 Prepared by Department of Preparatory year
Exercise Start the Python interpreter and use it as a calculator. The symbol for multiplication is *. If you run a 10 kilometer race in 43 minutes 30 seconds, what is your average time per mile? What is your average speed in miles per hour? (Hint: there are 1.61 kilometers in a mile). 19 Prepared by Department of Preparatory year
Part 1 References 20