Python_level_1 (1)
Python_level_1 (1)
Programming
(LEVEL 1)
2
Table of Contents
Test Your Skills ........................................................................................................................ 3
Questions: ............................................................................................................................ 3
Let’s Start Programming.......................................................................................................... 4
Variables.................................................................................................................................. 5
Arithmetic Operators .............................................................................................................. 6
Test Yourself ........................................................................................................................ 7
Data Types............................................................................................................................... 7
Syntax Errors ......................................................................................................................... 10
Logic Error ............................................................................................................................. 10
Systematic Approach to find errors ................................................................................... 11
Find the errors in the following program ........................................................................... 11
3
Explanation:
1. Greeting Message:
print("Hello, world!")
2. User Input:
name = input("What is your name? ")
The input function displays the prompt message and waits for the user to
enter their name. The input is stored in the name variable.
3. Personalized Greeting:
print("Hello”, name)
Questions:
Answer: It is the instruction to wait for the text a user enters and
to then make use of this
• Why is some text in speech marks and some not?
Answer: Speech marks indicate that the exact text will be used
when the program is run. No speech marks mean that the content
of that memory space is used
• What is the purpose of 'name'?
Answer: This is a variable to store the name the user inputs.
What is the difference between this program and a Scratch (or block-based) program?
Answer: There are no blocks, you have type everything, you cannot fit pieces of code
together, you have to decide what fits where. There is no list of code that you can
choose from, there is no visual display for images or text to be displayed.
Note:. If you discover errors, or if your program does not run, check with your pair and
carefully compare their program with the program you are displaying.
Experiment by:
Variables
• A variable is used to store data
• The value in the variable can change
• A variable has a name (identifier)
• You can access data from a variable
MyName = "Jamelia"
MyAge = 12
In this case, the variables are called ‘MyName’ and ‘MyAge’. Taking the ‘MyName’ variable
as an example, when the program is run this time, the data stored will be “Jamelia” but that
this could change the next time the program is run.
Note: there are some reserved words that cannot be used. These are words used by the
program, for example we cannot name a variable ‘print’, because this is a command word in
Python.
Here is a program that includes data stored in variables, and outputs them, for example:
MyName = "Fabrizio"
MyAge = 13
print("Hello", MyName)
print("You are", MyAge, "years old")
6
In Pairs explore the above code and answer the following questions. Are you ready ?
• What is the name of the two variables?
Answer: MyName and MyAge
• What data is stored in MyName?
Answer: "Fabrizio"
• Change the program so the name stored is "Wasim" instead. What change did you
make?
Answer: Deleted “Fabrizio” and wrote “Wasim”
• How many output statements are in the program?
Answer: Two
• What happens if you remove the speech marks, " ", from a print statement?
Answer: An error occurs.
• What is the purpose of the comma in the print statements?
Answer: It joins text with a variable so that in can be output
• Change the program so that “Wasim” is actually “15”. What change did you make?
Answer: We removed ‘13’ and put ‘15’.
• Add an extra variable to store Wasim's favourite colour. Output this colour in a
message, for example “Your favourite colour is purple”. What changes did you make?
Answer: A new variable was created and given a name, for example MyColour.
“purple” was stored in this variable. A new print statement was added with the text
"Your favourite colour is" and then a comma, and then the variable name.
• What happens if you change “Wasim” to “wasim”?
Answer: “wasim” is output
Arithmetic Operators
calculate the answers:
• 10 + 3
• 20 / 4
• 15 - 9
• 5*5
what do each mathematical operator mean.
7
for example:
Number1 = 10
Number2 = 20
Result = Number1 + Number2
print(Result)
Test Yourself
• change the operator to subtraction
• change the operator to division
• change the operator to multiplication
• add the two values, then multiply by 2
• create a third variable and store the number 30 in it
• add all three values together and output the result
• subtract two values from a third value and output the result
• multiply all three values together and output the result
• add the three values then multiply the total by 10
What is the difference between (1 + 2) * 2 and 1 + (2 * 2)? . From the output we can see
that the Mathematical BODMAS rule is applied here.
First = 1
Second = 2
Third = 2
Final = (First + Second) * Third
print(Final)
Change the position of the brackets in the statement and to identify the difference
between the values output.
Data Types
Integer, Real and String.
programs can store data of different types. Integer, Real and String
For example:
8
• 10
• blue
• 205
• 0.3
• horse
Group the displayed data according to Integer, Real and String.
How would you describe these groups?
One of the groups contains words, one contains whole numbers, one contains decimal
numbers.
These three groups are String (text), Integer (whole numbers) and Real (decimal numbers).
From the above data can you vote whether they are String, Integer or Real and the reasons
for your choice.
when writing programs, a programmer usually has to state what type of data a variable is
going to store. For example, if a variable is going to store a name, then it should be stated
in the program it is going to be a string. It will however be important for learners to
understand that Python does not require this, however. Python will allow different data
types to override a variable.
strings are surrounded by speech marks, that tell the computer to output the information
between the speech marks
only. For example:
• print(Value1) will output the content of Value1
• print("Value1") will output the word "Value1"
write the following code and then run it to see what it does: (file name: datatypes)
Number = 10 * "Bob"
print(Number)
Result = "a" + "b"
print(Result)
Result = 10 - 20.5
print(Result)
Result = 0.3 + 0.8
print(Result)
Result = 0.3 + "hello"
print(Result)
Result = 3 / "hi"
print(Result)
9
Result = 3 * "1"
print(Result)
Result = 3 * 1
print (Result)
output
1. Bob will output 10 times
2. "ab" is output
3. -10.5 is output
4. 1.1 is output
5. Error
6. Error
7. 111
8. 3
Reflection
• you cannot divide an Integer by a String
• you cannot add a string to an Integer or Real
• you can add, subtract, multiply and divide Integer and Real numbers
• you can multiply an Integer by a String and output that String a number of times
• when you add two Strings they join together.
When a user enters a value in Python, it automatically stores as a String. Therefore, if you
enter a number, it will not be stored as a number, unless you tell Python to do this.
store the data input as an Integer. For example:
Number = int(input("Enter a number"))
'int' stands for Integer, and the data in the brackets will be stored as an Integer if the data is
intended to be a number rather than a word.
Explain that a Real number in Python is known as a ‘float’ and give an example:
Number = float(input("Enter a number"))
Any value entered will be stored as a real number.
Syntax Errors
Find the syntax errors from the code below.
Logic Error
When logic error is present, the program will run but it does not do what it is supposed to.
The following program should add together the two numbers that are input, and output the
total.
Value == Number * 9
print (Number, " * 1 =", Value
Value = Number * 10
print (Number, " * 1 =", Value)
Value = Number * 11
print (Number, " * 1 =", Value)
Value = Number + 20
print (Number, " * 1 =", Value)