z Chapter 2 Data Types z Data Types

  • Slides: 9
Download presentation
z Chapter 2 Data Types

z Chapter 2 Data Types

z Data Types § How we categorize values stored in memory. § Numeric Data

z Data Types § How we categorize values stored in memory. § Numeric Data Types: § int -> integers § float -> real numbers or decimals § The command type(argument) will return the data type of the argument.

z Let’s try it type(1. 0) will return the data type, but you won’t

z Let’s try it type(1. 0) will return the data type, but you won’t see it because you didn’t tell the computer to print it. So…. print(type(2. 5)) print(type(1. 0)) What will happen?

z str Data Type § The str Data Type stores strings. You can store

z str Data Type § The str Data Type stores strings. You can store strings in variables just as you can numbers § Example: # Create variables to reference strings first. Name = “Bob” last. Name = “Dylan” Print(first. Name, last. Name)

z Input from a keyboard § When we want to read input from the

z Input from a keyboard § When we want to read input from the keyboard, we use the function called input variable = input(prompt) is the general format. Example: first. Name = input(“What is your first name? ”) last. Name = input(“What is your last name? ”) print(“It is nice to meet you, ” , first. Name, last. Name)

z Input is always a string § The input function will always return the

z Input is always a string § The input function will always return the user’s input as the str data type. § If you need to perform mathematical operations on the input, you must convert it to int or float. There are functions built in to do this. # input a number from the user string. Value = input(‘How many hours did you work this week? ’) hours = int(string. Value)

z Nesting the Functions # We can accomplish the same task in one line

z Nesting the Functions # We can accomplish the same task in one line hours = int(input(‘How many hours did you work last week? )) § This is called nesting fuctions. It takes the result of the input function ( a string) is passed as the argument of the int function. The result of the int function is then stored in hours

z Same Can Be Done With Float # input and convert to float pay.

z Same Can Be Done With Float # input and convert to float pay. Rate = float(input(‘What is your hourly wage? ’))

z Homework § Write a short program which will input a user’s first name

z Homework § Write a short program which will input a user’s first name (str), last name (str), age (int), and a decimal test score (float). Display each. § Sample output: First Name: Billy Last Name: Joel Age: 71 Test Score: 0. 95