Readings On Variables

You might also like

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

Course 2

Variables
A variable is used to store the information that will be needed by the program and allows
you to associate a name and content. For example, client_name can be associated
with "John Smith". A variable is an entity present in a problem whose value changes
and is dependent on the context in which the problem lies. So, in another context,
client_name can become "Richard Kent". A good practice is to choose a meaningful
variable name that is to say that allows to deduce the use that will be made of the value
associated with it.

Examples:

• age
• clientName
• productNumber

Rules for naming variables

• The name must be meaningful, it must inform us about what the variable
represents
• The first character is a letter or underscore.
• Other characters can be letters, the underscore character (_), or numbers
• A programming language keyword cannot be used as an identifier.
• No accents are used.
• The name of the variable cannot start with a number.
• The Python language is case-sensitive, which means that upper and lowercase
letters are not the same variable (the variable AGE is different from aGe, itself
different from age).
• One will choose a nomenclature for the whole project: either mixedCase or
lower_case_with_underscores

Constants

A constant is a kind of variable who never change value during the program execution.
Constants are usually written in all capital letters with underscores separating words.

Examples:

• NB_OF_STUDENTS
• TPS_PERCENTAGE

1
Basic data types

A constant is a kind of variable

Object type Usage Naming examples Values examples


Numbers Representation quantity, cost 10, -34, 0, 4.56
of a numeric
value
Strings Lets you store name, description, 'Bob' , 'John Smith'
any number of zipCode
characters
Booleans Used to isValid True , False
represent the
TRUE value or
the FALSE value
(these are the
only possible
values).

Assignments

• Allows you to give a value to a variable


• Represented by the symbol =

Examples:

• age = 42
• clientName = 'Mathieu Leclerc'
• sisterName = "Frédérique Leclerc"
• productCost = 5.76
• copyOfClientName = clientName

As you can see, it is possible to use either single quotes ( ' ) or double quotes ( " ) when
assigning a string to a variable.

Arithmetic operators

Mathematical operators can perform basic mathematical calculations. Here are the
operators we will use:

Symbol(s) Operation Examples


+ Addition 2+4 equals 6
3.4+2.3 equals 5.7
2.0+3.0 equals 5.0

2
- Subtraction 2-4 equals -2
3.4-2.3 equals 1.1
2.0-3.0 equals -1.0
X or * Multiplication 2*4 equals 8
3.4*2.3 equals 7.82
2.0*3.5 equals 7.0
÷ or / Division 4/2 equals 2
7.82/2.3 equals 3.4
7.0/2.0 equals 3.5
% Modulo (remainder of integer 4 % 2 equals 0
division) 7 % 2 equals 1
27 % 4 equals 3

Priority of operators

The operator priority defines which operation should be evaluated first. Here is the order
of priority of the operators mentioned in this document:

Priority Category Operators


Parenthesis (interior ones first) ( )
1 Power **
2 Multiplication/division X, *, ÷, /
4 Modulo (remainder of integer division) %
5 Addition/subtraction +, -
6 Affectation =

Parentheses can be used to change the order of operations or to highlight the order in
which they should be performed. The rules are the same as in mathematics.

Here’s some examples :


• 1+3*4 is evaluated like 1+(3*4) and equals 13
• (1+3)*4 equals 16
• result = 1+3*4 gives the value 13 at the variable result
• result = (1+3)*4 gives the value 16 at the variable result

Strings concatenation
Concatenation means put one string at the end of another string to create a longer and
more complete string. There are multiple ways to concatenate strings but we’ll start with
the two simplest for now.

Examples:

3
clientLastName = 'Leclerc'
clientFirstName = 'Mathieu'
sentence = 'Welcome ' + clientFirstName + ' ' + clientLastName

or

sentence = 'Welcome ', clientFirstName, ' ', clientLastName

Interactions with the user


Inputs: A program should recover/read data from the user. We will use the input
instruction for that.

Outputs: A program should provide data to the user (almost always via the screen).
We will use the print instruction for displaying text on the screen.

Examples:

print('Welcome')
name = input('What is your name ?')
print('Your name is ', name)
print('Your name is ' + name)

Note that the last two lines gives exactly the same result!

Type conversions

Every input from the user (using the keyboard) is always a string. Even if a number
is entered on the keyboard it will result in a string for the program. So we need to
change (convert) that string into a number if we want to use it for calculations.

The conversions must be made on the input. There are multiple possibilities but
we will focus on two for now:
• conversion to an integer à int
• conversion to a real à float

Example:

print('Welcome')
quantity = int(input('What is the quantity bought?'))
price = float(input('What is the unit price?'))
total = quantity * price
print('The total is ' + total)

4
Comments

Sometimes, you will want to add sentences to help you through your code. The
comments are non-executable part of the code, yet quite essential in a program. It
adds readability to your code.

To add a comment in your code, you should begin each line with the pound (#)
symbol followed by a single space. You can add a comment on multiple lines if you
want.

Example:

print('Welcome')
# Reading the data from the user
quantity = int(input('What is the quantity bought?'))
price = float(input('What is the unit price?'))
# Calculation and display result
total = quantity * price
print('The total is ' + total)

You might also like