0% found this document useful (0 votes)
16 views27 pages

Lab 1

Uploaded by

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

Lab 1

Uploaded by

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

PYTHON: EXPERIENCING

IDLE AND WRITING


SIMPLE PROGRAMS
Objectives
• To be able to use Integrated Development and Learning
Environment (IDLE) to program in Python
• To be able to understand and write Python statements to
• output information to the screen
• assign values to variables
• get numeric information entered from the keyboard
Python
• Created by Guido van Rossum, National Research
Institute for Mathematics and Computer Science in the
Netherlands in the late 1980s
• A scripting language based on ABC
• Supports structured programming and object-oriented
programming
• Easy to extend
• The latest version is 3.8.5
Integrated Development and Learning
Environment (IDLE)
• Support both Windows and Mac OS
IDLE
• An interactive environment (called “Shell”)
"Hello World!"
• Type the followings:

print("Hello World!")
print("Hello World!\n")
print("Hello\nWorld!\n")

• What do you see?


EXERCISE 1
Display your name and student ID on the screen. Skip
one line before printing the student ID
Input and Output
• Type the followings:

print("Hello World!")
name = input("What is your name? ")
Dennis
print(name)

• What do you see?


Input, Process, Output (IPO)
• Type the followings:

x = eval(input("Input an integer: "))


123
y = eval(input("Input another integer: "))
1001
sum = x + y
print(sum)

• What do you see?


Example Program:
Temperature Converter
• Problem: The temperature is given in Celsius, user wants
it expressed in degrees Fahrenheit
• Specification
• Input – temperature in Celsius
• Output – temperature in Fahrenheit
• Output = 9/5 x (input) + 32
Example Program:
Temperature Converter
• Design
• Input, Process, Output (IPO)
• Prompt the user for input (Celsius temperature)
• Process it to convert it to Fahrenheit using F = 9/5 x (C) + 32
• Output the result by displaying it on the screen
Example Program:
Temperature Converter
• Before we start coding, let’s write a rough draft of the
program in pseudocode
• Pseudocode is precise English that describes what a
program does, step by step
• Using pseudocode, we can concentrate on the algorithm
rather than the programming language
Example Program:
Temperature Converter
• Pseudocode:
• I - Input the temperature in degrees Celsius (call it celsius)
• P - Calculate fahrenheit as (9/5) x celsius + 32
• O - Output fahrenheit
• Now we need to convert this to Python!
• Besides entering each statement in IDLE, we can create a
source file with an extension .py that contains the Python
code
Example Program:
Temperature Converter
# convert.py
# A program to convert Celsius temps to Fahrenheit
# by: Susan Computewell

def main():
celsius = eval(input("What is the Celsius temperature? "))
fahrenheit = 9/5 * celsius + 32
print("The temperature is", fahrenheit, "degrees Fahrenheit.")

main()
Saving the program in a .py file.
• Programs are usually composed of functions, modules, or
scripts that are saved on disk so that they can be used
again and again
• A module file is a text file created in text editing software
(saved as “plain text”) that contains function definitions
• In our example, we’ll use convert.py when we save our
work to indicate it’s a Python program
• Remember that Python treats indentation seriously: each
indentation in a Python program must strictly consist of 4
spaces, no more and no less
Getting *.py to Run
• Under File in IDLE, select Open to open the source file
• Under the open file window, select Run -> Run Module or
Press F5 on the keyboard
• Do not forget to include the last line main() in your Python
program
• The detailed role of main() will be discussed later
Example Program:
Temperature Converter
• Here is the program behaviour

>>>
What is the Celsius temperature? 0
The temperature is 32.0 degrees Fahrenheit.
>>> main()
What is the Celsius temperature? 100
The temperature is 212.0 degrees Fahrenheit.
>>> main()
What is the Celsius temperature? -40
The temperature is -40.0 degrees Fahrenheit.
>>>
EXERCISE 2
Modify convert.py so that it will take the degree in
Fahrenheit and change it back to Celsius

How can we modify the output below?


>>>
What is the Celsius temperature? 100
The temperature is 212.0 degrees Fahrenheit.
The temperature is 100.0 degrees Celsius.
>>>
Naming the identifiers
• Names are given to variables (celsius, fahrenheit),
modules (convert), functions (main), etc
• These names are called identifiers
• Every identifier must begin with a letter or underscore
(“_”), followed by any sequence of letters, digits, or
underscores
• Note that “-”, “.” and many other symbols are not allowed
• Identifiers are case sensitive
• Identifiers cannot be Python’s keywords
Python’s keywords
Expressions
• The fragments of code that produce or calculate new data
values are called expressions
• A (numeric/string) literal, which is the simplest kind of
expression, is used to represent a specific value, e.g., 10
or "Dennis"
• A simple identifier can also be an expression.
• Simpler expressions can be combined using operators +,
-, *, /, //, **, %
• The normal mathematical precedence applies.
• Only round parentheses can be used to change the precedence,
e.g., ((x1 – x2) / 2*n) + (spam / k**3)
• Try "I" + "love" + "you"
EXERCISE 3
If you assign 5 to x and then type x, the shell
will return 5 to you. What if you type y but
without assigning any value to it before?
Assignment statements
• Simple assignment: <variable> = <expr>

<variable> is an identifier, <expr> is an expression

• The expression on the RHS is evaluated to produce a


value which is then associated with the variable named on
the LHS
Assigning Input
• The purpose of an input statement is to get input from the
user and store it into a variable.
<variable> = input(<prompt>)
• E.g.,

x = eval(input("Enter a temperature in Celsius: "))

• Print the prompt


• Wait for the user to enter a value and press <enter>
• The expression that was entered is evaluated and assigned to the
input variable
• Two kinds of input: character string or number
EXERCISE 4
o Prompt user for a number and then print it out using just
input(<prompt>)
o Prompt user for a number and then print it out using
eval(input(<prompt>))
o What is the difference between the two?
o Remove eval() from convert.py and does it still run?
Why?
Summary
• The Integrated Development and Learning Environment
(IDLE) to program in Python is introduced
• We have examined Python statements to
• output information to the screen
• assign values to variables
• get numeric information entered from the keyboard
END

You might also like