0% found this document useful (0 votes)
47 views8 pages

Datatypes and Variables

DATATYPES AND VARIABLES

Uploaded by

molaposk10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Datatypes and Variables

DATATYPES AND VARIABLES

Uploaded by

molaposk10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

Y10-01-CT3: Data types and

variables
Y10-01-CT3: Data types and variables

Learning objectives

In this lesson you will learn to:

•recognise primitive data types (int, real, char, string)


•define the term 'variable’
•create variables of all types with meaningful names
•view contents of memory (variable) in IDE.

For more information on this topic, and additional student activities,


refer to student book Topic 1.3.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT3: Data types and variables

Variables

We’ve seen variables in a previous lesson.


Then we defined them as a named container for a value.

The technical definition of a variable in programming is:

A value stored in memory that is accessed using a name or label.


It can be changed throughout program execution.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT3: Data types and variables

Variables and data types

Variables can contain all kinds of information. Let’s look at a few


examples:
What do you notice about how these variables
studentAge = 14 are named?
heightInM = 1.61
It is important to use meaningful names – this
orderPaid = True means that anyone can look at the code and
gender = ‘F’ know what the variable relates to.

Use camelCase to show the start of each word


within the variable name.

Do this in all the code you write to make it easy


for other people (and you) to read and
understand.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT3: Data types and variables

Data types
There are four main data types you need to be able to recognise and
use. These are known as primitive data types.

Data type Used for Example

integer whole numbers 42

real numbers with a decimal place 3.14

Boolean true or false only False

character single characters d

NOTE: Python uses floating NOTE: Python uses


point numbers to represent strings to represent
real numbers. You will use the characters. We’ll learn
keyword float to describe more about these in a
them. future lesson.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT3: Data types and variables

TypeError
We will encounter a new kind of error today:

Traceback (most recent call last):


File "C:/Users/T/PycharmProjects/variables.py", line 7, in <module>
print(billPaid + gender)
TypeError: unsupported operand type(s) for +: 'bool' and 'str'

This kind of error tells us that we cannot do the operation


required because the data type of the variable we are working
with is not correct.
In this case, we’ve tried to add a Boolean and a character.
There are many other kinds of errors you will experience as you
continue to construct more complex programs.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT3: Data types and variables

IDE tools: view contents of variables


Many IDEs have tools that help programmers.

One of these tools is the Inspect Variables tool.

In PyCharm the tool gives the read out


opposite.

You can see the data types and values of


each variable.
This will be useful later when the values of
your variables change and you want to
monitor them to make sure they have
the values you expect.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.
Y10-01-CT3: Data types and variables

Wrap up: you have learned how to…

 Recognise primitive data types.


• Integer, real, Boolean, character.
 Define the term 'variable’.
• A stored (in memory) value with an identifier.
 Create variables of all types with meaningful names.
• Identifiers need to make sense for the program they are in.
• Should be in camelCase.
 View contents of memory (variable) in an IDE.
• IDE tools such as Inspect Variables are really useful.

© Pearson Education Ltd 2020. Copying permitted for purchasing institution only.

You might also like