Wednesday 1 July 2015

6. Data Types, Variables and Input



You will learn about:


  • Data types
  • Variables 
  • User Input

Activity 5.2
Data types
Use the “type” function to find out the data types for these values.
>>>type("Fred")
>>>type(198)
>>>type(88.9)
>>>type(True)
>>>type(False)

Hint: Remember that True and False must start with a capital letter.
Activity 5.3
The type function returns the data type of an expression.
For each of these expressions, first predict the data type then use the type command to see if your prediction was correct.
Expression
Predicted data type
Type command
Result
“hello world”

type("hello world")

False

type(False)

15

type(15)

35.6

type(35.6)

-999

type(-999)

“15”

type(“15”)

“False”

type(“False”)

True

type(True)

0.001

type(0.001)


Activity 5.4
The interactive shell is a useful place to explore variables.
Copy and run this code in the interactive shell.
myName="Fred Smith"
myAge=14
print(myName,myAge)

Create a variable called myName and assign it to your name.
Create a variable called myAge and assign it to your age
Create a variable called myEyes and assign it to your eye colour.
Create a variable called myHeight and assign it to your height.
Write the commands to display on the screen your name, age, eye colour and height.
Hint: Remember that a single ‘=’ is used to assign a value to a variable.  
Activity 5.5
Variable names
A programmer is trying to decide on a valid name for a variable that represents a house number.
Which of the following variable assignments are valid? Why are the others not valid?

Valid or invalid variable name?
Reason why not valid
8HouseNumber = 288


houseNumber = 288


house Number = 288


house_number = 288


import = 288



What type of error do you get when using an invalid variable name?
Activity 5.6
Copy and run this code and explain the result.
# Programmer Amy Jones 12/8/2013
# adds two numbers
numberOne=15
numberTwo=23
answer=numberOne + numberTwo
print("The answer is “,answer)

Amend the program to add another variable called numberThree. Assign the value 76 to this variable. The program should all up all three numbers and print the result.
Activity 5.7
Data types in Python
Complete this table to describe the four data types.
Data type
Python abbreviation
Explanation
Example
integer
int


string
str


float
float


boolean
bool



Hint: Quotation marks are used to show the start and end point of a string, e.g. “this is a string”.

No comments:

Post a Comment