0% found this document useful (0 votes)
52 views

Lesson 1 - Variables and Data Types

This document discusses data types in Python. It covers integer, float, boolean, character and string data types. It explains variables and how they are used to store and represent values. Functions like float(), int() and str() are presented to demonstrate how to convert between data types. Examples are provided to illustrate variable declaration, assignment, and printing variable values and types in Python.

Uploaded by

Aisha Al-Mannai
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Lesson 1 - Variables and Data Types

This document discusses data types in Python. It covers integer, float, boolean, character and string data types. It explains variables and how they are used to store and represent values. Functions like float(), int() and str() are presented to demonstrate how to convert between data types. Examples are provided to illustrate variable declaration, assignment, and printing variable values and types in Python.

Uploaded by

Aisha Al-Mannai
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Teach Computer Science

GCSE Python

Variables and data


types

teachcomputerscience.com
2

Lesson Objectives

Students will learn about:


▪ The usage of different data types
▪ Using float(), int() and str() functions
▪ Writing programs to compute simple mathematical functions

teachcomputerscience.com
1.
Content

teachcomputerscience.com
4

Variables
▪ Variables are labels that are used to represent values stored in computer
memory.
▪ The declaration of variables in a program is essential in some
programming languages (C, C++, Java, etc.) as the correct amount of space
in memory is allocated by the compiler.
▪ Python is a dynamically typed language and variables are automatically
assigned data types when they are assigned their first value.
▪ The value of the variable can be changed during program execution.
teachcomputerscience.com
5

Why do we need variables?


Think of a variables as a container that stores data. Variables
allow us to access that data at a later point. (Store data for use
later)
They also allow us to save memory by not having to store the
data multiple times – the name of the variable is used as a
reference.
Easier to update values as you only need to change the value
stored in the variable – anywhere you use the variable name
automatically changes! (Code Reusability)

teachcomputerscience.com
6

Constants

▪ Constants are labels that are used to represent values stored in computer
memory that remain unchanged during the execution of a program.
▪ For example: pi(=3.142) is a constant used to calculate the radius and
circumference of a circle.

teachcomputerscience.com
7

Data types
▪ To allocate memory for a variable, a program must know how much
memory is to be allocated.
▪ The main types of data used in computer programming are:
a) Integer
b) Real
c) Boolean
d) Character
e) String
teachcomputerscience.com
8
Assignment Value

Integer data type Calling a variable

▪ A positive or negative whole number used in num=95


mathematical operations. print('Value = ',num)
▪ In Python, the = symbol is used to assign values to print('Type = ', type(num))
variables and constants. (assignment)
▪ Consider the Python code given that assigns an integer
value to the variable num.
▪ Using a print statement, the value of the variable and
data type of the variable is printed.
▪ The type ( ) query returns the data type of the variable.
teachcomputerscience.com
9

Real/Float data type


▪ A positive or negative number that allows num1=1.5
decimals and fractions in mathematical num2=11.001
operations.
print('num1 = ',num1 ,'num2 = ' ,num2)
▪ Consider the code given in which real
print('Type = ', type(num1))
values are assigned to variables.
▪ The values and types are printed.
▪ It can be noted that integer data types
cannot be used for numbers with
decimals.
teachcomputerscience.com
10

Boolean data type


switch= True
▪ A Boolean data type accepts only two
values: True or False. print ('Value = ',switch)

▪ Consider a switch that is turned on as print ('Type = ',type(switch))


given.

teachcomputerscience.com
11

Character data type


▪ A variable or constant that allocates my_char = 'A’
memory space for one character.
print('value = ',my_char)
▪ A character may represent an uppercase print('type = ',type(my_char))
letter, a lowercase letter, a digit or a
symbol.
▪ For example: S, d, 5, $, etc.
▪ Although a character variable may consist
of a digit, it cannot be used in calculations.
▪ The value of a character or string is
enclosed within single or double teachcomputerscience.com
quotes.
12

String data type


emailID = '[email protected]
▪ A variable or constant that allows several
characters in length. print('value = ',emailID)

▪ Can be a combination of text AND print('type = ',type(emailID))


numbers.
▪ The data type of a character and a string
are both the same ‘str’.
▪ This denotes that characters are also
treated as strings in Python.

teachcomputerscience.com
13
PYTHON: Java:
name = “Miss Doherty” string name = “Miss Doherty”

Variable declaration
▪ Python is a dynamically typed programming language. The variables are
assigned data types when the first data is entered to it.
▪ In some programming languages, it is essential to declare variables
before assigning values to them.
▪ For example, in visual basic, a number is declared as integer and then
initialised as given,
Dim num As Integer = 0

teachcomputerscience.com
14

Variable declaration
▪ Whereas, in Python, we directly assign value to the variable and the
computer decides what data type is suitable for the variable. Therefore,
in Python, the statement is simply,
num = 0

teachcomputerscience.com
15

Converting values

▪ Data can be converted from one data type to another using:


• float(): converting to float data type
• int(): converting to integer data type
• str(): converting to string data type

teachcomputerscience.com
16

Using float()
▪ Consider the code below in which an integer is
converted to a floating-point number.
num1=-1
num2=float(num1)
print('num1 = ',num1 ,'num2 = ' ,num2)
print('Type of num1= ', type(num1))
print('Type of num2= ', type(num2))

teachcomputerscience.com
17

Using int()
▪ Consider the code given below that converts a
floating-point number to an integer.
num1=-31.7913
num2=int(num1)
print('num1 = ',num1 ,'num2 = ' ,num2)
print('Type of num1= ', type(num1))
print('Type of num2= ', type(num2))

teachcomputerscience.com
18

Using str()
▪ Consider a variable that stores a phone
number. Initially, it is assigned an integer
data type. A programmer converts its data
type to a string using the str() function.
var=9781299152
varNew = str (var)
Mathematical operations cannot
print('var = ',var ,'varNew = ' ,varNew)
be performed on variables of
print('Type of var= ', type(var)) string data types.
print('Type of varNew= ', type(varNew))
teachcomputerscience.com
19
Why phone numbers cannot
be stored in numerical data
types?
▪ An integer or float data type cannot be
used to store phone numbers. This is
because leading zeros in number data
types are not permitted.
▪ For example, if we try to assign var =
0123457891, this will raise a syntax error.
▪ Therefore, phone numbers are always
stored using a string data type.
teachcomputerscience.com
20

Naming identifiers
▪ While naming variables and constants, it is important to follow the given
guidelines:
• Follow the naming conventions of Python such as beginning variable
names with lowercase letters. For example, number, value, variable,
length, width and breadth.
• Use all uppercase letters for naming constants. For example, PI. This
way programmers can make sure that this value is not changed
throughout the program. NAME, TAX,
• Use meaningful names for variables. For example, use total and
average instead of x and y. teachcomputerscience.com
21

Naming identifiers
• Do not use long names. For example, tempC and tempF for
temperature in Celsius and temperature in Fahrenheit respectively.
• Use camel caps to separate words. For example, totalSales and
averageSales. Another way to separate words in variable names is to
use an underscore. For example, total_sales and average_sales.
• All variable names are case-sensitive. For example, tempCelsius is
different from tempcelsius.

teachcomputerscience.com
22

Reserved keywords and exec not as


• The list of reserved finally or assert for
keywords in Python are pass break from print
given in the table.
class global raise continue
• Reserved keywords
if return def import
cannot be used to name
identifiers. try del in while

elif is with else

lambda yield except

teachcomputerscience.com
23

Program to calculate area


and circumference of a circle

▪ Constant pi is assigned the value PI=3.142 #constant Pi


of 3.142. radius = 3
#calculating perimeter
▪ Value is assigned for variable
perimeter=2*PI*radius
radius.
#calculating area
▪ The area and perimeter are area = PI*radius*radius
calculated and are displayed on #printing values
the screen. print("Perimeter of circle is: %.2f" %perimeter)
▪ print("Area of circle is: %.2f" %area)
The symbol # is used to insert
comments.
teachcomputerscience.com
24

Program to calculate area


and circumference of a circle
PI=3.142 #constant Pi
radius = 3
#calculating perimeter
perimeter=2*PI*radius
#calculating area
area = PI*radius*radius
#printing values
print("Perimeter of circle is: %.2f" %perimeter)
print("Area of circle is: %.2f" %area)

teachcomputerscience.com
25

Let’s review some concepts


Variables Constants Data types
• Integer: positive or negative
Variables are labels that are used Constants are labels that are
whole number.
to represent values stored in used to represent values stored
• Real: positive or negative
computer memory. in computer memory that
number that allows decimals
remain unchanged during the
and fractions.
execution of a program.
• Boolean: accepts only two
Naming identifiers values: True or False.
• Character: uppercase letter,
Converting values • Names start with lowercase
lowercase letter, digit or
• float() letters.
symbol.
• int() • All uppercase letters for • String: allows several
constants. characters in length.
• str()
• Use meaningful names.
• Camel caps to separate words.
• Variable names are case-
sensitive.
teachcomputerscience.com

You might also like