Revision
In Tutorial 1:Hello World Program, we've learnt how to print the words. In Tutorial 2:Doule Quote or Single Quote, we've written two program that prints I'm Aung Aung. (one program using double quotes and one using double quotes)
Review Exercise 1:What happens if you leave out both the quotation marks?
Review Exercise 2:Write a program that prints 5 (by using either single quotes or double quotes). What happens if you leave out both the quotation marks?
Review Exercise 3: What function is used to display the words to the user?
In Review exercise 1, you will get error message. But in Review Exercise 2, you won't. This is because 5 and "I'm Aung Aung" belong to different data types: 5 is an integer while "I'm Aung Aung" is a string. In Python, the print() function doesn't need any quotes to print the integer value, but it needs either two single quotes or two double quotes to print the string value.
A value is one of the basic things a program works with. It may be a letter or a number. Some of the values we have seen are 'Hello World', 5, '5' and "Python for everyone".
Different values belong to different types: 5 is an integer and "I'm Aung Aung" is a string. If we want to know the data type of a value, we can use the type() function.
Now let's find out what type 5.0 belongs to. In your program, write
type(5.0)
and run the program(click enter if you are using command promp). You don't get anything because the type() function only finds out what type it is and doesn't display the result to the user.
Now we have to answer Review Exercise 3 to see the type of 5.0 . The answer is the print() function. Since we need to print the result of type(5.0), we have to use the code
print(type(5.0))
and run it. The output is <class 'float'> , which means that the data type of 5.0 is floating-point number, or simply 5.0 is floating-point number. In this result, class is used in the sense of category. (A type is a category of values.)
Exercise1: It is better to make mistake noe and on purpose than later and accidentally. You can learn what error messages mean. It also helps you remember what you read. Now make some mistakes and answer the following question.
- What happens if you leave out one or both quotes i.e. if you run the code print(type(I'm Aung Aung))
Exercise2: Use the type() and the print() function to answer the following questions.
- What is the type of 20?
- What is the type of 20.0?
- What is the type of "20"?
- What is the type of '20'?
- What is the type of "20.0"?
- What is the type of '20.0'?
- What is the type of "Python for Everyone"?
Comments
Post a Comment