Professional Documents
Culture Documents
Python
Python
You can type code into both the interactive window and the editor window. The difference
between the two windows is in how they execute code.
The interactive window opens automatically when you start IDLE. You’ll see the following text,
with some minor differences depending on your setup, displayed at the top of the window:
This text shows the version of Python that IDLE is running. You can also see information about
your operating system and some commands you can use to get help and view information about
Python.
The >>> symbol in the last line is called the prompt. This is where you’ll type in your code.
At the prompt in the interactive window, type the word print followed by a set of parentheses
with the text "Hello, World" inside:
1
Unit 3 Computational thinking: Programming languages
A function is code that performs some task and can be invoked by a name. The above code
invokes, or calls, the print () function with the text "Hello, World" as input.
The parentheses tell Python to call the print () function. They also enclose everything that gets
sent to the function as input. The quotation marks indicate that "Hello, World" really is text and
not something else.
The interactive window executes a single line of code at a time. This is useful for trying out small
code examples and exploring the Python language, but it has a major limitation: you have to
enter your code one line at a time!
The interactive window stays open when you open the editor window. It displays the output
generated by code in the editor window, so you’ll want to arrange the two windows so that you
can see them both at the same time.
Before you run your program, you need to save it. Select File Save from the menu and save the
file as hello_world.py. The .py extension indicates that a file contains Python code.
Every time you run code from a file, you’ll see something like the following output in the
interactive window:
2
Unit 3 Computational thinking: Programming languages
You can also open a file from a file manager, such as Windows Explorer or macOS Finder.
Right-click the file icon and select Edit with IDLE to open the file in IDLE’s editor window.
Create a Variable
Variables are fundamental to programming for two reasons:
1. Variables keep values accessible: For example, you can assign the result of some time-
consuming operation to a variable so that your program doesn’t have to perform the
operation each time you need to use the result.
2. Variables give values context: The number 28 could mean lots of different things, such as
the number of students in a class, the number of times a user has accessed a website, and
so on. Giving the value 28 a name like num_students makes the meaning of the value clear.
3
Unit 3 Computational thinking: Programming languages
variable = value
Get input
To get user input in Python you use command input. The following Python command will get
user input and store it as a variable called name:
The text inside the brackets is called the prompt. The prompt tells the use what to input. Can
you see that the prompt includes a space after the question mark? That means there will be
space between the text and the user’s input.
Show output
To produce output in Python you use the command print. After this command you insert
brackets. Whatever you put inside the brackets will be output.
For example, you might want to output:
a number or calculation
a string (that means some text in quote mark)
the name of a variable.
4
Unit 3 Computational thinking: Programming languages
A computer stores all data using a digital code. For example, text characters are stored using
ASCII code. Python has different data types:
ASCII characters are stored as string data type.
Whole numbers are stored as integer data type.
Number with a decimal point are stored as float data type.
For example:
x=5 type(x) Output: <class ‘int’>
y=2.5 type(y) Output: <class ‘float’>
Str (String) - Strings in python are surrounded by either single quotation marks,
or double quotation marks.
Input data
In Python, user input is stored as a string.
Input data is stored as a string.
String cannot be used in calculations.
5
Unit 3 Computational thinking: Programming languages
Convert to integer
A Python function called int converts any variable to integer data type. The command looks like
this:
variable = int (variable)