1 / 9

Data Types

Data Types. Integer 15 String HelloThere! Float/Real 15.625 Boolean Yes / No Char P. Strings. Greeting = ‘Hello World’ print (Greeting) Hello World Letter = Greeting[2] print (Letter) l. Strings Len Function. Use to find out length of string. Name = ‘Dave’

cvanwinkle
Download Presentation

Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Types Integer 15 String HelloThere! Float/Real 15.625 Boolean Yes / No Char P

  2. Strings Greeting = ‘Hello World’ print (Greeting) Hello World Letter = Greeting[2] print (Letter) l

  3. Strings Len Function Use to find out length of string Name = ‘Dave’ len(Name) 4

  4. Strings converting data types To convert from one data type to another, python has some useful functions 23 #an integer Str(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 = 15 print(Age – 2) 13 e.g.2. Age converted to a string Age = 15 print(str(Age) – 2) ERROR! # can’t take a number value away from a string!

  5. Strings converting data types e.g.4. Concatenate a string and integer Age = 15 Statement= ‘You are’ Statement + Age ERROR!!! …try Statement +str(Age) Statement15 Or better still Statement + ‘ ‘ +str(Age) You are 15

  6. converting data types We can also try converting to integer data type Age = ’99’ type(Age) <class ‘str’> …try Type(int(age)) <Class ‘int’> Obviously, you can’t convert ALL data to type integer! e.g Greeting = ‘Hello World’ Type(int(Greeting)) Error!!! #letters don’t correlate to a number!

  7. converting data types We can also try converting to integer data type to a Floating data type (i.e. a REAL number) Age = 99 type(Age) <class ‘int’> …try NewAge = float(Age) Print(NewAge) 99.0 e.g. Pi = 3.14 type(Pi) <class ‘float’>

  8. 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

  9. 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))

More Related