Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
104 views

Module 1: Programming Fundamentals

The document introduces programming fundamentals using Python. It discusses why Python is a good first language, describes console programs and how to use IDLE. It explains how Python code is compiled and run, and distinguishes between syntax errors and runtime errors. The document provides several activities to write simple Python programs, run them in IDLE, and debug syntax and runtime errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

Module 1: Programming Fundamentals

The document introduces programming fundamentals using Python. It discusses why Python is a good first language, describes console programs and how to use IDLE. It explains how Python code is compiled and run, and distinguishes between syntax errors and runtime errors. The document provides several activities to write simple Python programs, run them in IDLE, and debug syntax and runtime errors.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Module 1: Programming

Fundamentals
Objectives

 Understand why Python is a good first language for new programmers.

 Describe a console program.

 Use IDLE to test Python expressions and statements in the interactive shell.

 Use IDLE to open, compile, and run a Python source file.

 Learn how Python compiles and runs a program in terms of source code,
bytecode, and the virtual machine.

 Distinguish between syntax errors and runtime errors.


What is programming?

 Ideas?

 Programming is the process of creating a set of step-by-step instructions that


tell a computer to perform a task.

 Programming languages allow you to communicate with the computer.

 Computers understand only Machine language which is sequence of 0s and


1s
Programming Languages

 Machine languages: sequences of 0s and 1s


 Assembly languages: use short words known as “mnemonics” to write
program
 Must be translated by assembler
 Still considered low-level languages
 High-level languages: more human readable languages that allow
programmers to create programs without knowing how CPU works.
Popular high-level programming languages

 C++

 Java

 C#

 Javascript

 Python
Each Programming language

 Has its own (limited) vocabulary called keywords:

 keywords have special meaning and can be used only in a specific way.
 Keywords include Operators that represent special program functions such as addition,
subtraction, and multiplication

 Has its own grammar rules referred to as syntax:

 Syntax is a set of rules that must be strictly followed to write computer-understandable


instructions
Why Python is a great first language

 Python has a simple syntax and is easy to read and use.

 Python can be used to create console applications, GUI applications, games,


and web applications.
A console application
A GUI application
A web application
Why Python is a great first language (contd)

 Python is portable across many operating systems.

 Python is open source and has a large active developer community.

 Python is popular, is used by many successful companies, including Google,


IBM, Disney, and EA Games.
IDLE: Python’s IDE (Integrated Development Environment)
How to use the interactive shell

 Enter Python code after the >>> prompt. Then, press Enter.

 If you enter valid instruction that produces a result, the shell displays the
result.

 If you enter an invalid instruction, the shell displays an error message.


A console application that’s being run in the shell
Activity: Try IDLE shell

 Open IDLE shell

 Type some expressions involving arithmetic operators (+, - , *, /)

 Type print command:

print(“Hello World”)
How to create, open, save, and close source files
 Use IDLE’s File menu and common techniques for your operating system.

How to switch between source and shell windows


 Use IDLE’s Window menu.

How to enter and edit Python code


 Use IDLE’s Edit and Format menus, common editing keystrokes, and context menus.
Activity: Create HelloWorld.py

 In IDLE create a new file

 Write Python code to print “Hello World” and “Have a fabulous day”

 Hint: Use the print command


 Save the program to file HelloWorld.py

 Run the program

 From the editor window, press the F5 key or select RunRun Module.
 If IDLE displays a dialog box that indicates that you must save the program first, click Yes to save
it. Then, if the program doesn’t have any errors, IDLE runs the program in the interactive shell.
Activity: Create HelloWorld.py (contd)

 Sample run
Activity: Run happynewyear.py

 Download happynewyear.py program from Module 1

 Open it in IDLE

 Run the program using F5

 Understand the parts of the program.


Built-in functions

 print : A built-in function to display text on the console

print(“Hello World” + “!!!”)

 input: A built-in function to read input from the console

name = input("Please enter your name: ")

 help: A useful utility to look-up documentation on any


of python object
Activity: Create Registration.py

 In IDLE, create a new file and write a new Python program that

 Prints a greeting: “Registration Form”


 Asks user: “Your name: ” (assume user entered John)
 Reads the value, remembers the name
 Asks user: “Your secret code: “ (assume user entered Superhero)
 Reads the value, remembers the code
 Prints the output:
Welcome John
Your registration is complete
Your password is JohnSuperhero

 Save the file as registration.py and Run the file using F5


Activity: Registration (contd)

 Sample run:
Compilers and Interpreters

 A compiler translates a high-level language program to a separate


machine program for CPU to read and execute
 Source code: the statements a programmer writes in a high-level language
 Compilation: translates a text-based source code to binary codes
 C/ Fortran/ C++/C# / Java are compiled languages.

 Interpreter reads, translates, and executes the instructions of a high-


level language program
 Examples are PHP, Perl, Python, and ASP.NET
How Python compiles and runs source code
Procedure

 Step 1 The programmer uses a text editor or IDE to enter and edit the
source code. Then, the programmer saves the source code to a file with a .py
extension.

 Step 2 The source code is compiled by the Python interpreter into bytecode.

 Step 3 The bytecode is translated by the Python virtual machine into


instructions that can interact with the operating system of the computer.
Syntax Error vs Runtime Error

 Syntax error:

 It is caused by invalid syntax. Statements are not formatted correctly.


 Python compiler cannot compile the program (and hence cannot run it!)
 Runtime error:

 Syntax is valid, so program can be compiled.


 But at runtime, program does something that is not allowed by the computer.
 Program terminates (crashes) and a message is displayed.
A dialog box for a syntax error
A message that’s displayed for a runtime
error
Activity: Fix errors

 Download future_value_errors.py from Module 1

 Open in IDLE and try running it

 Fix all the errors encountered.


Activity: FeetToMeters.py

 Write a new Python program called FeetToMeters.py

 It should:

 Prompt the user input for number of feet as integers


 Read and save the feet
 Convert feet to meters using the formula:
 Meters = feet * 0.3048

 Prints the number of meters on screen


Activity: FeetToMeters.py (contd)

 Sample run:

You might also like