![Page 1: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/1.jpg)
Data Types
Integer 15String HelloThere!Float/Real 15.625Boolean Yes / NoChar P
![Page 2: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/2.jpg)
Strings
Greeting = ‘Hello World’print (Greeting)Hello World
Letter = Greeting[2]print (Letter)l
![Page 3: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/3.jpg)
Strings Len Function
Name = ‘Dave’len(Name)4
Use to find out length of string
![Page 4: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/4.jpg)
Strings converting data types
23 #an integerStr(value) will convert a given value to a string.Str(23) # is now a string ‘23’
e.g.1. Age as an ordinary integer.Age = 15print(Age – 2)13e.g.2. Age converted to a stringAge = 15print(str(Age) – 2)ERROR! # can’t take a number value away from a string!
To convert from one data type to another, python has some useful functions
![Page 5: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/5.jpg)
Strings converting data typese.g.4. Concatenate a string and integerAge = 15Statement= ‘You are’Statement + Age ERROR!!!
…try Statement + str(Age)Statement15
Or better stillStatement + ‘ ‘ + str(Age)You are 15
![Page 6: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/6.jpg)
converting data typesWe can also try converting to integer data typeAge = ’99’type(Age)<class ‘str’>
…try Type(int(age))<Class ‘int’>
Obviously, you can’t convert ALL data to type integer!e.gGreeting = ‘Hello World’Type(int(Greeting))Error!!! #letters don’t correlate to a number!
![Page 7: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/7.jpg)
converting data typesWe can also try converting to integer data type to a Floating data type (i.e. a REAL number)Age = 99type(Age)<class ‘int’>
…try NewAge = float(Age)Print(NewAge)99.0
e.g.Pi = 3.14type(Pi)<class ‘float’>
![Page 8: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/8.jpg)
Prompting User for some Data
We will use input() to grab some data from our end user.Age = input(‘What is your Age? ’)print(Age)’99’ # note…this is a string
Converting this into an integer can be done by…?
Age = int(input(‘What is your Age? ’))print(Age)99 # note…this is a now an integer
![Page 9: Data Types Integer 15 StringHelloThere! Float/Real15.625 BooleanYes / No CharP](https://reader036.vdocuments.mx/reader036/viewer/2022082612/56649ea95503460f94bad679/html5/thumbnails/9.jpg)
Prompting User for some Data
TASK•Prompt the user for 5 numbers. •Each number should be assigned an appropriate Variable name e.g. Num1, Num2 etc•After 5numbers have been entered, the sum of the numbers should be outputted as
‘The sum of your 5 numbers is x’•The final line should show the average of the 5 numbers, and should read like this
‘The average of your 5 numbers is x’((Think carefully about which data types to use in the Sum and Average calculations))