Python

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

INTRODUCTION TO

PYTHON
1. Introduction to variables. Input and Output of a program
1.1. Variables. Introduction
As we know, a computer program performs the following actions:
● the input of data inside the computer
● process of the data
● the output of data outside the computer

A variable is a reserved space in the computer’s memory that will contain the value indicated in the
program

For example, see the following program written using Python as a programming language:

name = "Maria"
age = 13
print(name,"has",age,"years old")

, in which age and name are two variables. This means that every time the programmer uses the
words age and name he/she refers to values 13 and Maria unless they were changed.
A variable, in a simple way, contains two differentiated parts:
● the identifier, that is the name of the variable such as name or number
● the value, that is the data that is contained inside the variable such as Rose or 4

Also, a variable can be shown as a box, which has a name (the box’s name or identifier) and a value
inside it, such as in the following picture, in which the name Maria is the value of the variable name.

The most important thing regarding variables is that, although the data inside a variable can be
changed, a variable is able to contain only one value at a time. This means that the variable Name can
only contain the name Maria but not the names Maria and Tom at the same time.
1.2. Rules to write identifiers

Like another programming language, Python indicates a formal way to write identifiers and we must
follow the rules of the language. Those rules are the following:

● use a word that makes sense for the data stored in the variable such as name, number,
password, … etcetera rather than use x, y, z, t and so on.

● the identifier must be written in lowercase. Due to this, name or number is a correct identifier
whilst Name or NAME is a bad way of writing identifiers

● In case you need to use compound names for identifiers, there is no space allowed between
words and you must place an underscore. For example, Number of players is a bad-written identifier.
It must be number_of_players.
1.3 Data Types: TYPES DESCRIPTION EXAMPLES
As we know, a variable Integer a whole number 2; -10
contains a value. This
value can be one of Float a number with decimal places 2.56 ; -5.89
the following types. String a sequence of one or more characters a ; Hello

Boolean a data that can contain only two values called True or False True; False

● if a variable stores the user’s name, the data type must be -------- because is a --------------------------------(in
this case, letters to compose a name)
● if a variable stores if the user has passed the last Computing exam, the data type must be -----------
because the decision only has two possible values: ------ (if the user has passed the exam) ------------(if the
student hasn’t passed the exam)
● if a variable stores the user’s age, the data type must be --------- because the age is normally stored using
----------------------
● if a variable stores the user’s weight, the data type must be -------- because the weight is normally
measured using ----------------------
1.3 Data Types: TYPES DESCRIPTION EXAMPLES
As we know, a variable Integer a whole number 2; -10
contains a value. This
value can be one of Float a number with decimal places 2.56 ; -5.89
the following types. String a sequence of one or more characters a ; Hello

Boolean a data that can contain only two values called True or False True; False

● if a variable stores the user’s name, the data type must be String because is a sequence of characters (in
this case, letters to compose a name)
● if a variable stores if the user has passed the last Computing exam, the data type must be Boolean
because the decision only has two possible values: True (if the user has passed the exam) or False (if the
student hasn’t passed the exam)
● if a variable stores the user’s age, the data type must be Integer because the age is normally stored using
whole numbers
● if a variable stores the user’s weight, the data type must be Float because the weight is normally
measured using decimal numbers
● 1.4. Changing values inside variables
● As we know, a variable is able to contain only one value at a time but is easy to change the values when
we want. We only need to know that the last value is the value that will be contained inside the variable
and the other will be “deleted”. Take a look at the following example:

● number = 2
● number = 5
● number = number*3
● number = number - 3
● number = number/3
● print(number)

● At the end of the program, what is the value stored in the variable number? ------
● 1.4. Changing values inside variables
● As we know, a variable is able to contain only one value at a time but is easy to change the values when
we want. We only need to know that the last value is the value that will be contained inside the variable
and the other will be “deleted”. Take a look at the following example:

● number = 2
● number = 5
● number = number*3
● number = number - 3
● number = number/3
● print(number)

● At the end of the program, what is the value stored in the variable number? is 4
Activity 1

1. A). Define the following terms: variable, value, identifier, data type

1. B). Indicate if each of the following identifiers has been correctly written. In
case they don’t, write a well-written identifier using Python’s rules.
●Name
●username
●Number 1
●user_Password
●width_rectangle
●UserPassword
●PASS
• At the end of the program, what is the value stored in the variable number? is 4
1. C). For each of the following values, write a well-written identifier name:
●the average mark in Maths
●the number of players in a game
●the color of eyes of a character in a game
●the mark of a Computing exam
●the data that stores if a switch is on or off

1. D). For each of the values of the last exercise, guess its data type (integer,
float, boolean, string)
Activity 2

2. A). Indicate if each of the following identifiers has been correctly written. In
case they don’t, write a well-written identifier using Python’s rules.
●User
●Subject_1
●123456
●Element_Color
●width shape
●NumberSTUDENT
●password
• At the end of the program, what is the value stored in the variable number? is 4
2. B). For each of the following values, write a well-written identifier name:
●the number of students at year 10A
●the color of pencils in a pencil case
●the size of a banana in a bowl
●the position in which a runner has placed in a race
●the data that stores if an answer is yes or no

2. C). For each of the values of the last exercise, guess its data type (integer,
float, boolean, string)
ACTIVIY 3. Guess the final result of each of the following programs written using
Python as a programming language.
number = 2 number_one = 20
number = number/2 number_two = 5
number = number + 1 number_one = number_one/4
print(number) number_one = number_one*number_two
print(number_one)

Result: Result:

name = “Mona” char1 = “x”


name = “Lisa” char2 = “#”
name = name + name char3 = “$”
print(name) char4 = “¡”
username = “Joe”
lastname = “Stevenson”
password = char1+lastname+char4+”
“+char3+username+char2
print(password)

Result: Result:
ACTIVIY 3. Guess the final result of each of the following programs written using
Python as a programming language.
number = 2 number_one = 20
number = number/2 number_two = 5
number = number + 1 number_one = number_one/4
print(number) number_one = number_one*number_two
print(number_one)

Result: 2 Result: 25
name = “Mona” char1 = “x”
name = “Lisa” char2 = “#”
name = name + name char3 = “$”
print(name) char4 = “¡”
username = “Joe”
lastname = “Stevenson”
password = char1+lastname+char4+”
“+char3+username+char2
print(password)

Result: Mona Lisa Result: X Stevenson ¡ $ Joe #

You might also like