# Data types

## Write the formula in Python

• Write programs that calculates and displays the answers to the following formulas. Remember that when you see something like P = IV, this is the same as P = I × V. Create your own values to test each of them (e.g a = 10, b = 20):
1. x = a + b
2. P = IV
3. F = ma
4. E = mc2

## Write the formula in Python

• Write programs that calculates and displays the answers to the following formulas. Remember that when you see something like P = IV, this is the same as P = I × V. Create your own values to test each of them (e.g a = 10, b = 20):
1. x = a + b
2. P = IV
3. F = ma
4. E = mc2
``````a = 10
b = 20
c = 15
I = 1
V = 2
m = 20

x = a + b
P = I * V
F = ma
E = m * c * c

print(x)
print(P)
print(F)
print(E)``````

## Learning Intentions

• Develop our understanding of variables

• Understand how to use comments within our programs

• Know the different data types

## Success Criteria

• I can develop my own program using variables
• I can develop a program that performs checking the data types of variables

## Variables in Python

``````print("Please insert a number")
y = int(input())
z = 2

x = y * z

print(x)
``````

Write a very short three-line program that will ask the user "Please insert your name" then take in a user’s name and stores it in a variable and then display "Hello " and the user’s name.

Step 2: Get the user’s input
Step 3: Display the "Hello " and the user’s input

• One of the most important things in programming, especially if you choose to do Computing Science in National 4/5, is internal comments.
• A comment is a piece of text within the program that describes what your code does. Python ignores comments when it is run.
• In Python, a comment line starts with a hashtag sign #
• It is important to note that comments only go across one line, so each line after that will need a # at the start too

• We should get used to using comments so that when we look back at our programs, we can understand what each piece of code does without needing to look it up.

``````#Display to the user Please insert a number
#Get the number from the user and put it in y
y = int(input())
z = 2

#Do the calculation
x = y * z
#Print the result
print(x)``````

## Data types

• A data type is the type of the data you are storing in a variable.
For instance, when we are storing a number using the int(input()) function we are getting an integer.
Python has 5 data types we need to know about.

## Why do we need to know about data types?

• Understanding how a computer stores data is important for us because we sometimes need to convert the data to another type. For example, if we use the `input()` function we have been using in the past examples the computer expects text even if we want a number.

• To convert the text into a number so that we can use it in calculations we use the `int()` function. This tells the computer that it should now consider that string as an integer.

## Data type conversion

• We have multiple ways of converting data that we are going to use for the next part of this lesson:

## Data type conversion

• Converting the data is possible using the functions `str`, `int` and `float`.

``````x = "10"
print(x)

y = int(10)
print(y)``````

## Data types

• For each of the following pieces of data, write down the data type you would expect the value variable to be:

String

value = "Hello world"

Character

value = "0"

Boolean

value = True

Float

value = 0.5

String

value = "101Paris"

Character

value = "c"

## Why do we need to know about data types?

• Understanding how a computer stores data is important for us because we sometimes need to convert the data to another type. For example, if we use the `input()` function we have been using in the past examples the computer expects text even if we want a number.

• To convert the text into a number so that we can use it in calculations we use the `int()` function. This tells the computer that it should now consider that string as an integer.

• Computers store data in the memory, but without the program knowing what data type is being used it's just a bunch of 1s and 0s.

10101100 10111100 01110111

## Checking the type

• Python has a very useful way of checking the data type of a variable using the `type()` function.
• For example:
``````name = "Jack"
print(type(name))
age = 12
print(type(age))
``````

Your task is to create a program that displays the type of three different variables.

To do this, you will need to create three variables and use three `type` functions to get their types. What do you notice about their types when they are displayed?

## tESTING THE TYPE FUNCTION

``````var1 = 11.3
print(type(var1))``````

Once you have done that you can go back to your computer and create a program to test these types using the `type()` function.

You can continue with the tasks up to page 16 in the Python booklet.

Presentation Overview
Data types
© 2020 - 2024 J Balfour
05:08 | 22-07-2024
!
Keywords
DragonDocs Management
Random selector
Sections
Binary conversion
Denary to binary conversion
Binary to denary conversion
Feedback 👍
Accessibility

Apply a filter:

×
All slideshow files