Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

Python_level_1 (1)

Uploaded by

aaravkumbhat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Python_level_1 (1)

Uploaded by

aaravkumbhat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

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

Test Your Skills


print("Hello, world!")
name = input("What is your name? ")
print(“hello”, name)

Refer File Name “1.Hello world”

Explanation:

1. Greeting Message:
print("Hello, world!")

This prints a simple greeting message.

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:

• What does the program do?


Answer: It outputs ‘Hello world!’, asks a user to enter their name,
then says ‘Hello and name’
• What do you think 'print' means?
Answer: It is the instruction to output what is in the brackets
• What do you think 'input' means?
4

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.

Let’s Start Programming


print("This is my first message")
print("This is my second message")
print("This is my last message")

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:

• changing the text that is output


• removing one or more lines of output
• adding more lines of output.
create a list of what you now know about outputting in Python. each pair to
share one item from their lists
• there is a ‘print’ command word
5

• ‘print’ is written in lowercase


• the output message is in speech marks
• after ‘print’ there is an open bracket
• at the end of the message there is a closed bracket
• each print statement is on a new line

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

Storing data in a variable, for example:

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.

In Python, the command for converting to a String is ‘str’, for example:


10

Number = str(input("Enter a number"))


Any value entered will be stored as a String.

Syntax Errors
Find the syntax errors from the code below.

Animal = INPUT("Enter the name of an animal")


Colour = input(Enter a colour)
output("I've never seen a" Colour Animal)

Run the program and identify the errors.


• the first ‘INPUT’ should be lower case
• the second input should have speech marks, “ “, around the Enter a colour
• the comand word in the final line should be print rather than output
• in the ‘print’ command, the output text and the two variables need to be separated
with a comma, ‘,’.
• The correct program should be:
• Animal = input("Enter the name of an animal")
• Colour = input("Enter a colour")
• print("I've never seen a", Colour, Animal)

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.

Number1 = input("Enter a number")


Number2 = input("Enter another number")
Total = Number1 + Number1
print(Total)

Find the error?


11

The calculation adds Number1 to Number 1 and not to Number 2.


This error is an example of a logic error. Write down the term ‘logic error’. When a logic
error is present, the program will run but it does not do what it is supposed to.

Systematic Approach to find errors


• Look at each line of code, one at a time
• Check the words used were correct
• Check that each mathematical symbol, or operator, was correct
• Check that the correct variables are used
• Check that speech marks were used where needed.
• It is important to note that using a systematic approach, such as checking each line
one-by-one, helps to make sure all parts of the program are checked. This is different
from skipping to a mathematical operation and ignoring all other areas, because the
error can occur anywhere in the program, and there might be several errors.
Find the errors in the following program
Create a checklist of errors that you identify during this activity, including the process or
processes that they used for finding them. Add this to your programming guide and add
further errors in the future. The checklist should include the types of error that can occur
and examples of what they look like.

Number = input("Enter the times table you want to view")


Value = Number * 1
print (Number, " * 1 =", Value)
Value = Number * 2
print (Number, " * 1 =", Value)
Value = Number * 3
print (Number, " * 1 =" Value)
Value = Number * 4
print (Number, " * 1 =", Value)
Value = Number * 5
print (Number, " * 1 =", Value)
Value = Number * 6
print (Number, " * 1 =", Value)
Value = Number * 7
print (Number, " * 1 =", Value)
Value = Number * 8
print (Number, " * 1 =", Value)
12

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)

You might also like