Lab 1
Lab 1
print("Hello World!")
print("Hello World!\n")
print("Hello\nWorld!\n")
print("Hello World!")
name = input("What is your name? ")
Dennis
print(name)
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