Python Program Structure
We will use a simple program to explain variables, data types, input/output and assignment statements.
1 # In replit, the default file name is main.py
# To rename a Python program in replit, Click on the 3 dots on the right side of main.py
2 # You can name this program to convert.py, compile and run in the Shell window
3 def main():
4 print("This program converts Celsius to Fahrenheit.")
5 c = eval(input("What is the Celsius temperature? "))
6 f = 9/5*c + 32
7 print(c, "Celsius is ",f, "in Fahrenheit.")
8 main()
Some sample output:
This program converts Celsius to Fahrenheit.
What is the Celsius temperature? 20
20 Celsius is 68.0 in Fahrenheit.
This program converts Celsius to Fahrenheit.
What is the Celsius temperature? 12
12 Celsius is 53.6 in Fahrenheit.
20 is an integer numeric type, and 53.6 is a float numeric type. Data types will be explained in the later section of the lab.
What is Python Function?
A function is a block of organized and reusable code used to perform a single, related action.
Python has many built-in functions like input(), print(), eval(), str() and etc. User-defined functions are the functions you created by yourself.
You can define functions to provide the required functionality. Function blocks begin with the keyword def followed by the
function name and parentheses(). We will talk about functions in detail later.
For a list of Python built-in functions, here is the list.
https://www.w3schools.com/python/python_ref_functions.asp
What is the differences between function and method?
While learning Python, we have seen Python functions, like main() in the first lab. Later we will see Python methods.
We know a Python function is a sequence of statements execute in a certain order. We talked about built-in
functions like eval() and print(). There are also the user-defined functions we will describe later.
What are the differences between functions and modules in Python?
Python functions have been used since the first lab. A function is a block of organized, reusable code that is used to perform a single related action.
They are two types of functions, user-defined and built-in functions. Built in functions are provided by Python in coding like print(), input(), eval() etc.
A module is a file that has a collection of useful functions and can be imported as a whole file into any application.
You can use it in multiple projects and share it with other programmers.
Modules have .py extension.
https://rwet.decontextualize.com/book/functions/
https://www.codeleaks.io/function-vs-module-in-python/
What is Python method?
Python method is like a function, except it is attached to an object. Detail with examples are discussed in later labs.
What is Python object?
Python is an object-oriented programming language. An object is a collection of data (variables) and
methods (functions). Everything in Python is treated as an object, including variable, function, list, tuple, dictionary, set, and etc.
Every object belongs to its class.
For example - An integer variable belongs to integer class.
An object is the collection of various data and functions that operate on those data.
Python Classes
A Class is like an object constructor, or a "blueprint" for creating objects.
Here are a few links with examples if you want to explore more on the concept of functions, methods, objects and classes.
https://www.w3schools.com/python/python_classes.asp
https://docs.python.org/3/tutorial/classes.html
https://www.youtube.com/watch?v=noNcqLLjOwE
https://www.tutorialspoint.com/python/python_functions.htm
https://realpython.com/python3-object-oriented-programming/
Variables and Data Types
Variables
Variables are used to give names to values. All names are called identifiers.
Unlike other programming languages, Python has no command for declaring a variable.
https://www.w3schools.com/python/python_variables.asp
c and f are variables in the convert.py program.
num1, num2 and average are variables in the avg.py program. This program is listed below in the lab
assignment section.
Rules for Python variables:
- A variable name MUST start with a letter or the underscore character
- A variable name CAN NOT begin with a number
- A variable name can ONLY contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
Expressions
The program code block that produces or calculates new data values are called expressions. The simplest kind of expression
is a literal. A literal is used to indicate a specific value. The convert.py contains 9, 5, and 32. These are examples of
numeric literals.
Data Types
Variables can store data of different types, which can do different things. The
convert.py introduced numeric type and text type.
Textual data is strings. You can think of a string as a sequence of printable characters. A string literal
is indicated in Python by enclosing the characters in quotation marks (" ").
Python has seven built-in data types: Text, Numeric, Sequence, Mapping, Set, Boolean and Binary. The detail is here.
https://www.w3schools.com/python/python_datatypes.asp
Basic Input and Output
Input
The input() function allows user input from the keyboard.
https://www.w3schools.com/python/ref_func_input.asp
https://www.programiz.com/python-programming/input-output-import
Output
The print() function prints a message to the screen.
https://www.w3schools.com/python/ref_func_print.asp
https://docs.python.org/3/tutorial/inputoutput.html
Assignment Statement
The basic assignment statement has this form:
<variable> = <expr>
Assigning a string to a variable.
https://www.w3schools.com/python/gloss_python_assign_string_variable.asp
Assign a value to multiple variables
https://www.w3schools.com/python/gloss_python_assign_value_to_multiple_variables.asp
When the user input is a number, we need a more complicated form of input statement:
<variable> = eval(input(<prompt>))
You need to use the eval() function to input a number instead of some text (a string).
Lab Assignment
1. Study the following program. Pay attention to syntax on line 10. It illustrates the use of multiple-input on one line.
#Student Name:
#Student #:
#replit username:
#Lab2 assingment:
#main.py in replit, unless you name it to avg.py
#You can use any Python IDEs to write this program
#This program computes the average of two numbers.
def main():
print("This program computes the average of two numbers.")
num1,num2 = eval(input("Input two numbers separated by a comma: ")) #line 10
average = (num1+num2)/2
print("The average of two numbers is: ", average)
main()
Sample output:
This program computes the average of two numbers.
Input two numbers separated by a comma: 23,12
The average of two numbers is: 17.5
2. Write a Python program to ask a user to input two numbers.
Display both numbers along with the sum of the two numbers and the product of the two numbers.
There are
many ways to write this program. However we would like you to use eval(), input() and print() functions
you learned from this lab. Your program output should look similar to the following:
Sample Output:
This program computes the sum and the product of two numbers.
Enter two numbers separated by a comma: 12,23
The sum of 12 and 23 is: 35
The product of 12 and 23 is: 276
Rename the program from main.py to lab2.py
Compile and run the program from the Shell Window.
Demonstrate your completed program to your lab instructor if the lab is in person.