0% found this document useful (0 votes)
73 views

Chapter 1 Slides

Uploaded by

Sage Hudak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Chapter 1 Slides

Uploaded by

Sage Hudak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter 1

An introduction to
Python programming

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 1
Objectives
Applied
1. Use IDLE to test Python expressions and statements in the
interactive shell.
2. Use IDLE to open, compile, and run a Python source file.
Knowledge
1. List three reasons why Python is a good first language for new
programmers.
2. Describe a console program.
3. Explain how Python compiles and runs a program in terms of
source code, bytecode, and the virtual machine.
4. Explain how main memory and disk storage work together when a
program is running.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 2
Objectives (cont.)
5. Distinguish between systems software and application software.
6. Distinguish between testing and debugging.
7. Distinguish between syntax errors and runtime errors.
8. Describe an exception.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 3
Four general-purpose programming languages
 C++
 Java
 C#
 Python

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 4
The Python timeline
Year Month Release
2000 October 2.0
2008 December 3.0
2010 July 2.7
2015 September 3.5

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 5
Syntax differences between Python and Java
Some Java code
private static double calculateFutureValue(
double monthlyInvestment,double monthlyRate, int months) {
double futureValue = 0.0;
for (int i = 1; i <= months; i++) {
futureValue =
(futureValue + monthlyInvestment) * (1 + monthlyRate);
}
return futureValue;
}

Python code that works the same


def calculateFutureValue(monthlyInvestment, monthlyRate, months):
futureValue = 0.0
for i in range(months):
futureValue =
(futureValue + monthlyInvestment) * (1 + monthlyRate)
return futureValue

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 6
Why Python is a great first language
 Python has a simple syntax that’s easier to read and use than most
other languages.
 Python has most of the features of traditional programming
languages. As a result, you can use Python to learn concepts and
skills that apply to those languages too.
 Python supports the development of a wide range of programs,
including games, web applications, and system administration.
 Python is used by many successful companies, including Google,
IBM, Disney, and EA Games. As a result, knowing Python is a
valuable skill.
 Python is open source. There are many advantages to being open
source.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 7
A console application

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 8
A GUI application

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 9
A web application

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 10
The source code for a console application
#!/usr/bin/env python3

import locale

# set the locale for use in currency formatting


result = locale.setlocale(locale.LC_ALL, '')
if result == 'C':
locale.setlocale(locale.LC_ALL, 'en_US')

# display a welcome message


print("Welcome to the Future Value Calculator")
print()

choice = "y"
while choice.lower() == "y":
# get input from the user
monthly_investment = float(input("Enter monthly investment:\t"))
yearly_interest_rate = float(input("Enter yearly interest rate:\t"))
years = int(input("Enter number of years:\t\t"))

# convert yearly values to monthly values


monthly_interest_rate = yearly_interest_rate / 12 / 100
months = years * 12

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 11
The source code for a console application (cont.)
# calculate the future value
future_value = 0
for i in range(months):
future_value = future_value + monthly_investment
monthly_interest_amount = future_value * monthly_interest_rate
future_value = future_value + monthly_interest_amount

# format and display the result


print("Future value:\t\t\t" + locale.currency(
future_value, grouping=True))
print()

# see if the user wants to continue


choice = input("Continue? (y/n): ")
print()

print("Bye!")

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 12
How Python compiles and runs source code

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 13
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.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 14
Main memory and disk storage
as an application runs
Main memory Disk storage

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 15
How disk storage and main memory work
together
 When you start the computer, it loads the operating system into
main memory. Then, you use the features of the operating system
to start an application.
 When you start an application, the operating system loads it into
main memory. Then, it runs the application.
 As the application runs, it may read data from disk storage into
main memory or write data from main memory to disk storage.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 16
IDLE’s interactive shell

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 17
How to open, close, and restart the shell
 To start IDLE, use the features of your operating system. This
opens an interactive shell.
 To close the interactive shell, click on its close button or select
FileClose.
 To restart an interactive shell, select RunPython Shell from an
interactive shell window.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 18
How to use the interactive shell
 Enter Python code after the >>> prompt. Then, press Enter.
 If you enter valid code that produces a result, the shell displays the
result.
 If you enter an invalid code, the shell displays an error message.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 19
IDLE’s editor with a source file displayed

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 20
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.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 21
A console application that’s being run in the shell

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 22
How to compile and run a Python 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.

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 23
A dialog box for a syntax error

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 24
A message that’s displayed for a runtime error

© 2016, Mike Murach & Associates, Inc.


Murach's Python Programming C1, Slide 25

You might also like