0% found this document useful (0 votes)
5 views4 pages

Python Variables and Data Types Explained

Uploaded by

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

Python Variables and Data Types Explained

Uploaded by

jihad2533
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercise 1 - Variables and data types

At the most general level of description what most programs do is work based from the data you
have given them.

As a result we need to store data and do something with it.

The storing is referred to a DATA Structures


The doing is referred to as ALGORITHMS.

Data Structures
Constants.
This is where a value does NOT change. This could be something like VAT which is fixed at 20% and
so we will always use a set value for our calucaltions.
Variables.
This is where our value will change. E.g. the length of our rectangle which we had set at 12. We
might not always want it to be this value so the name(identifier) length won’t change but our value
could.

Data types

Individual pieces of data in a Python program can only be of a certain limited range of
types. The data types we will be using are:

int – integer (number with no fractional part) e.g. 3, 100000000, -25


float – a floating point number e.g. 3.065, 10.0, -25.5
A floating point number is a computer representation of a real number.
It sometimes has to limit the number of significant digits.
str – a series of characters (letters, digits, punctuation, spaces) which might be a
word. In programming this is called a string. (str)
bool – has only two possible values, True or False. It is short for Boolean.

Variables in Python do not have a set specified type unlike other programming languages which
makes it easier. If name = 100 the program would read this as int/but if name = Bob the program
would read this as str.
Exercise 1.

Specification

Store and display some information about an animal –


its name, how many legs it has, its height in metres and whether it has a tail.
The algorithm for this task might be:

 store animal’s name data


 store animal’s leg data
 store animal’s height data
 store animal’s tail data
 display animal’s name data
 display animal’s leg data
 display animal’s height data
 display animal’s tail data

When coded in Python it might look like this:

name = "Elephant"
numLegs = 4
height = 4.5
hasTail = True
print(name)
print(numLegs)
print(height)
print(hasTail)

Which part is setting up the variables?


Type this code into a new window in your IDE. Save it and run it.

This code will produce the output


Elephant
4
4.5
True

The equal's sign "=" is the assignment operator. Think of it as "gets the value of".

After the statement name = "Elephant" is performed, any use of the identifier
name will produce the string "Elephant".

The second statement declares assigns the value 4 to an integer variable called
numLegs.

Imagine being somebody looking at the output without being able to see the code. It
wouldn't make a lot of sense. Anyone looking at it may have no idea what 4, 4.5 or
True refer to.
How the user experiences a program is called the user interface. It is a very important consideration
in the programming process.

The code below shows how to add text to label each piece of data.

print("Name:", name)
print("Number of legs:", numLegs)
print("Height in metres:", height)
print("Has a tail:", hasTail)
Try it!!

This code should produce:


Animal name: Elephant
Number of legs: 4
Height in metres: 4.0
Has a tail: True

Comments

Imagine being someone else looking at the code. What is its purpose? Who wrote it?
When? This information should be recorded in comments within the code. Comments
are notes in the code written for the programmer. In some languages these comments have //
before them but Comment lines in Python are preceded by a #.

Extension

Change the statements in the code example to describe

• a chicken
• a spider
• a fish

AND DON’T forget from now on…..USE YOUR COMMENTS!!!!

You might also like