This site is from a past semester! The current version will be here when the new semester starts.

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!


  1. Open the IDLE tool.
  2. Choose FileNew File
  3. 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!')
    
  4. Save the file by choosing FileSave as. You can give a name such as hello.py
  5. RunRun Module (F5). The output will show up in the IDLE shell.

Writing your first Python program




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


Try your own


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'>

Try your own


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

Try your own


Explanation of a similar Python program


Exercise: Length of Names

Exercise : Length of Name

Write a program to read a name from the keyboard and print the length of the name. Follow the example below:

What is your name?
John Doe
The length of your name is 8

In addition to functions print and input, you may need the len function e.g., len('abc') returns 3.