Computer basic knowledge
Computer basic knowledge
PA05 Unit1
Introduction to
Python
1.1 About Python
print("Hello Python")
• To open IDLE’s built-in code editor, click the File menu of the
Python Shell window and select New File .
• To execute an opened
Python document, we
may also use the
shortcut key F5.
2. The data type of the same variable can change as different values are
assigned to it.
• When a variable is created, the program will assign a memory location for its use, with its name
being the identifier of the location.
Contents
Task 3: Using comments
When writing a Python program, you can insert a hash sign (#) as a
single-line comment delimiter. Any content after it, treated a
comment, won’t be executed. Comments serve to help other users
understand a program.
• To look up the full list of Python keywords, enter the following commands in the Python Shell:
import keyword
print(keyword.kwlist)
Contents
2. Determine if the following strings are valid Python variables. Circle the
correct answers.
3. Write down the output results of the commands below. • In the Python Shell,
we may place
multiple commands
(a) x=2.88e6; print(x) _____________________
2880000.0 in the same line by
using semicolons(;)
(b) x=3.14e-3; print(x) _____________________
0.00314 as separators among
them.
Contents
(b) Strings
Strings are for recording textual data. To include string data in a
program, enclose them in a pair of single quotes or double quotes as
in the following:
1.6 1.7
Hello, Jerry
1.8 ______________________
Contents
4. Why doesn’t the command print('Hello, '+name) output “Hello, name”?
As name in the command is a variable, the program outputs the
_________________________________________________________
value stored in the variable, which is the user’s input value received
_________________________________________________________
by the input() function, namely, “Jerry”.
_________________________________________________________
“What is your favourite food?”. The program will wait for the user to
input the answer before outputting a response based on the answer
input, “[ ]! Oh, me too!” as shown in the following example:
1.9
Contents
1.3.4 Operators
Programming often involves some types of operations in which operators are
used. Common types of Python operators include arithmetic operators,
comparison operators and logical operators.
• An operator is a symbol or keyword for performing an operation on operands. An
expression is a statement made up of operators and operands.
Indentation of conditional blocks is a must in Python language while it is optionally used for style
and readability in many other coding languages.
There are three types of conditional structures for if statements. Below are
their syntax, flowcharts and tasks.
Start/End symbol
Process symbol
Decision symbol
1.11
Pass.
___________________
• Every line of a conditional block in an if statement must be indented by the same
whitespace. Instead of typing spaces before each line, it is preferable to indent each line
with the Tab key to guarantee uniform indentation.
Contents
B. if…else conditional structure
The if…else conditional structure evaluates if a specific conditional
expression is true. If true, the program will execute Block 1 right after the
conditional expression. Otherwise, the program will execute Block 2 right
after else.
1.13
When you run the above program, you will encounter an issue: no matter
what number you input, the program always returns an error. The reason is
that the number input by the user through the input() function is
assigned to the variable mark as a string instead of a number.
Contents
For example, when the user inputs 60, the variable will be assigned the
value '60'. When the condition mark >= 50 is evaluated, since the two sides
of the comparison operator have different data types, the program will end
with an error. To solve it, use Python’s built-in function float()to convert the
string data into a number data first. Modify the above program
“u1_task6.py” by assigning value to mark as follows:
mark = float(input())
• Python provides built-in functions int(), float(), str() for data type conversion among
integers, floating-point numbers and strings.
For example:
float('4') returns 4.0
int(4.1) returns 4
str(123) returns '123'
Now run the modified program. If the input number is greater or equal to
50, the output result will be “Pass.”; otherwise, “Fail.”
Contents
C. if…elif…else multiple-condition structure
This conditional structure is suitable for sequentially evaluating which of the
multiple conditional expressions is true. In Figure 1.14, the program first
evaluates if conditional expression 1 is true. If true, Block 1 will be executed.
Otherwise, conditional expression 2 is evaluated. If true, Block 2 will be
executed. Otherwise, Block 3 will be executed.
For simplicity’s sake, here we only include a single elif conditional expression between i f and else. In
practice, it is common to have multiple elif expressions between if and else.
1.15
Contents
9. Modify the program “u1_task7.py” so that it can evaluate
which grade the input mark belongs according to the table
below.
Contents
Task 8: Using a Python program for LED on/off control
Run the above program. During program execution, the computer will display a prompt for input:
Enter Y/N/B; Y:on; N:off; B:off and exit. If Y or y is input, the LED will turn on. If N or n is input, the
LED will turn off. If B or b is input, the LED will turn off and the program will end.
Test the program in this procedure: When prompted to input, first enter Y to turn on the LED. Then
enter N to turn off the LED. Finally enter B to end the program.
Contents
About Python
1. Python currently ranks among the most popular coding
languages for
novices and have a wide range of applications such as web
development, hardware automated control, data analysis and
AI.
1.18
____________________________________________________________
If the age input is greater or equal to 60 or less than 6, a 50% discount
____________________________________________________________
message will be outputted; otherwise, a 10% discount message outputted.
2. What will be outputted for the following command input in the Python
Shell?
s1='2'; s2='4'; print(s1+s2)
24
______________
Contents
C. Enrichment
Add an LED to the circuit in Figure 1.15 in this Unit. Then create, save and
run the program in Figure 1.19. Test the program for four different execution
results with different input values (1,2,3,4).
1.20
1.19
-End-
Contents
Program file:u1_task2.py
Back
Contents
Activity Window 1 answer:
print('Who are they?')
print('Jane, Amy, Ada')
Back
Contents
Program file: u1_task4.py
Back
Contents
Program file: u1_q5.py
Back
Contents
Hints:
1. 5<8 and 7>2 are both True
2. 10<8 and 9<5 are both False
3. 4<=7 is True, and so the operand of not is True
Back
Contents
Program file: u1_task5.py
Back
Contents
Program file: u1_task6.py
Back
Contents
Program file: u1_task7.py
Back
Contents
Activity Window 9 answer:
print('Please input your mark.')
mark = float(input())
if mark >= 90:
print('A.')
elif mark >= 80:
print('B.')
elif mark >= 50:
print('C.')
else:
print('Fail.')
Back
Contents
Program file: u1_task8.py
Back
Contents
As Python IDLE editor does not show line numbers, the teacher is advised
to use a web-based Python editor which shows line numbers when
presenting a long program to the class for teaching effectiveness. An
example is https://trinket.io/python (as in Figure 1.17)
Back
Contents
Program file: u1_ex.py
Back