Python Programs
Scripts (Programs)
While the interactive shell is good for testing out small code bits, longer Python programs (also called scripts) are written as text files with the .py
extension. Python reads the program and executes statements from top to bottom.
Let's create and run a Python program:
Press the button to run the code (don't worry about the meaning of the code). You can edit the code and run again!
- Open the IDLE tool.
- Choose
File
→New File
- In the new window that opens up, enter the following code (don't worry about the meaning of the code).
print('Hello world!') print('Bye!')
- Save the file by choosing
File
→Save as
. You can give a name such ashello.py
Run
→Run Module (F5)
. The output will show up in the IDLE shell.
Comments
You can use comments to provide more information about the code to the reader. Any text that follows #
(until the end of the line) is considered as a comment by the Python interpreter, and is ignored.
# this is a simple hello world program
print('Hello') # start with a greeting
# read user's name from the keyboard
print('What is your name?')
name = input()
Blank lines too are ignored by the Python interpreter. You can use blank lines to improve the of the code e.g., by using a blank line to separate one group of statements from another
Using Functions
A function is like a small program that you can execute from another program. A function has a name. Some functions take inputs (called arguments). The statement to execute (other names: call, invoke) a function takes the form function_name(arguments)
e.g., foo(1, 'hello')
calls a function named foo
with arguments 1
and 'hello'
Python comes with many built-in functions you can use right away.
To print some text, you can use the print
function which can take one or more arguments:
>>> print('Hello World')
Hello World
>>> print('Bye', 'my', 'friend')
Bye my friend
print('Hello World')
print('Bye', 'my', 'friend')
Hello World
Bye my friend
Some functions return a value.
int
, float
, str
functions can be used to convert a data value of one type to another. The built-in function type
tells you the type of a value.
age = 25 # age is of integer type
print('My age is ' + str(age)) # convert age to a string before printing
print(type(-4)) # print the type of value -4
print(type('-4')) # print the type of value '-4'
print(type(float('5.0'))) # convert string '5.0' to a float and print the type
My age is 25
<class 'int'>
<class 'str'>
<class 'float'>
input
function can be used to read input from the keyboard. It waits for user input (until the user hits Enter) and returns all text entered by the user as a string. Note how a call to the print
without any arguments (i.e. print()
) prints an empty line.
# read current price
print('What is the current price?')
price = input() # price is of type string because input() always return a string
# print current price
print() # print an empty line
print('Current price is $' + price) # no need to convert; price is already a string
# calculate and show discounted price
discounted_price = float(price)*0.5 # convert to float type first
print('Price after 50% discount is $' + str(discounted_price)) # convert back to string