CSC103 Variables Types Operators
CSC103 Variables Types Operators
A typical program uses various values that change during its execution. For example, we create a
program that performs some calculations – lets say, calculate the amount of money we have earned
from the last month. Now we need a place to keep those values and then use it to determine our
profit. That container which we keep the result of our calculation is called variable.
In every program, the values entered by one user will obviously be different from those entered in by
another user. This means that when creating the program, the programmer does not know what
values will be introduced as input, and that makes it necessary to process all possible values a user
may enter. When a user enters a new value that will be used in the process of calculation, we can
preserve it (temporarily) in the random access memory of our computer. The values in thispart of
memory change (vary) throughout execution and this has led to their name – variables.
[ ]: courseCode = "CSC103"
courseTitle = "Introduction to Python"
examScore = 95
age = 45
From the snippet above, we have created three variable, (can you name them?). Notice the format
which it is being done.
[name of container] = [a value to insert into the container]
We give our variables (containers) distinct names so that we can be able to identify them. Keep in
mind that variable names are always unique – they should never be any two variables with the
1
same name in a program and they are case sensitive. So age = 30 is different from Age = 30 and
different from AGE = 30. These are three different containers namely age, Age and AGE.
Can you create some more variables? Write them in your workbook.
1.2.2 Conventions for Variable Naming
Now notice how we named our variables. The variable names clearly states what it is holding. This is
a good naming practice that you should follow. Also, notice how we capitalize the first letter of every
word constituting our variable name. This makes our variable names clearly readable.
This is called pascal case naming convention. There are many naming conventions like:
• Only the beginning letter is capitalized [Sellingprice]
• Every first letter of a word is capitalized – [CostPrice]
• Words are joined with underscore [cost_price, selling_price]
It is a good practise to adopt a given convention and maintain it throughout your code.
Python uses dynamic typing and as such does not require the programmer to predifine the data
type. In creating data types we define the type of container which we want to create. Take for
instance we want to store bread in a container, we would definitely look for a container which is big
enough and durable to hold our bread, this could be a basket or some big box. But take for instance
we want to store water in that same container, lets say the same basket we used to store the bread,
it’s clear that a basket cannot store water so we have to look for an alternative- these types of
containers are called data types.
A data type is a category for values, and every value belongs to exactly one data type. The most
common data types in Python are Integers, floating-point numbers and Strings. The values -67 and
7, for example, are said to be integer values. The integer (or int) data type indicates values that are
whole numbers. Numbers with a decimal point, such as 73.19 , are called floating-point numbers (or
floats). Note that even though the value 42 is an integer, the value 42.0 would be a floating-point
number.
Python programs can also have text values called strings, or strs. String values are always sur
rounded by quote-marks ( ’ ’ or ” ” ) as in (’Hello’ or ’Great Claretians!’) in order to let Python know
where the string begins and ends. There can be an empty string in which case we have only the
quote (’ ’ or ” ”). No characters in-between ’ ’. Python does not pertmit you to combine the two
quotation style. ”Today” is acceptable but ’Today” is not.
1.2.4 Operators
Python, like every other programming language uses operators to perform different operations on
data.
You have learned how to declare and set a variable in the previous earlier on, we will discuss how to
perform various operations using variable and operators.Operators allow processing of primitive
2
data types and objects. They take one or more operands as input and return some value as a result.
Operators in Python are special characters (such as ”+”, ”.”, ”^”, etc.) and they perform
transformations on one, two or three operands.
Examples of Python operators are the signs for addition, subtraction, multiplication and division from
math (+, -, *, /) and the operations they perform on the integers and the real numbers.
Python operators can be categoried as follows: a. Arithmetic operators (-, +, , /, %, ++, --) – used to
perform simple mathematical operations b. Assignment operators ( &&, ||, !, ^) – allow assigning
values to variables. c. Comparison operators (&, |, ^, ~, <<, >>) – allow comparison of two literals
and/or variables. d. Logical operators (==,!=, >, <, >=, <= ) – operators that work with Boolean data
types and Boolean expressions. e. Binary operators (=, +=, -=, =, /=, %=, &=, |=, ^=, <<=, >>= ) –
used to perform operations on the binary representation of numerical data.
Some operators have precedence (priority) over others. For example, in math, multiplication has
precedence over addition. The operators with a higher precedence are calculated before those with
lower. The operator () is used to change the precedence and like in math, it is calculated first. The
precedence of the operators in Python, from highest to the lowest is: **, %, //, /, *, -, +
costPrice = 10003
sellingPrice = 12000
# Change the value of the costPrice so that the program can output "Sorry you␣ ,→made a loss"
[ ]: