# Chapter 2 - Data types and Standard I/O ###### tags: `Computer programming` ## Learning Objectives By the end of this chapter, you should able to: 1. List out the rules and types for the identifiers 2. List, describe and use the primitive data types in Python 3. Create and use variables and contents in Python programs 4. Describe the modifiers for standard input and output ## Identifiers An identifier is used to name the **data and other objects** in a Python program. Below table summarizes the rules for identifies 1. First character must be alphabetic characters (A - Z, a - z) or underscore (\_). 2. Must consist only of alphabetic characters (A - Z, a - z), digits (0 - 9) or underscores (\_). 3. Consist at most 79 characters 4. Cannot be a keyword / reserved word. The reserved word is listed in Appendix I. Here are some examples of identifiers. | **Valid identifiers** | **Invalid identifiers** | | --- | --- | | `a # valid but poor style` | `$num # $ is illegal` | | `student_name` | `2names # first char digit` | | `_aSystemName` | `sum-salary # contain hyphen` | | `_bool # boolean system id` | `stdnt nmbr # contains space` | | `INT_MIN # System defined value` | `int # keyword` | As Python is a case-sensitive language, the identifier name is also case-sensitive. Hence, the identifier `apple` is different from `Apple`. A good identifier name is short but descriptive by making the work together by `CamelCase` or `combine_by_underscore`. ## Data types The _data type_ defines the set of values and the set of operations that can be applied on those values. For example, the integer type has values such as -1, 0, 1, 2, 3, etc. Addition (+) and subtraction (-) can be applied to two integers. In Python, there are 2 categories of data types, namely _primitive types_ and _derived types_. The derived type would be discussed in the later chapters. ![](https://i.imgur.com/1Am9HYp.png) ### Primitive data types Python has four primitive types, namely _Boolean_, _Integer_, _Float_ and _Boolean_. #### Boolean (`bool`) Booleans are used to represent truth values with two constant objects True and False. The built-in function for converting an object to Boolean is bool(). ```python!= num = 1 print(bool(num)) ``` <iframe src="https://trinket.io/embed/python3/2d350e5354?runOption=run&start=result" width="100%" height="100" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> #### Integer (`int`) Just as in mathematics (where it is referred to as a signed integer) an integer is a _whole number_ that could hold a zero, positive or negative value. You can convert any decimal number, octal, hexadecimal or string to an integer by using the `int()` function. ```python!= decimal_number = 23.5 print(int(decimal_number)) ``` <iframe src="https://trinket.io/embed/python3/bfe8ae9674?runOption=run&start=result" width="100%" height="100" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ```python!= year = 1999 print(int(year)) ``` <iframe src="https://trinket.io/embed/python3/95d9091db0?runOption=run&start=result" width="100%" height="100" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> #### Floating point (`float`) Float represents real numbers like 3.1415, a data type that is used to define floating decimal points. To check if something is a float, we can use the `isinstance()`. ```python!= print(isinstance(4.5, float)) ``` <iframe src="https://trinket.io/embed/python3/b699e7f076?runOption=run&start=result" width="100%" height="100" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> As a general rule integers don't have a decimal point, whereas floats do, so 19 and 19.0 have the same value but they are different types. Using the `float()` function each string or integer could be changed to a float. ```python!= num1 = 55 print(float(num1)) num2 = "313" print(float(num2)) ``` <iframe src="https://trinket.io/embed/python3/61e60453a5?runOption=run&start=result" width="100%" height="130" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> The `round()` function returns the provided number rounded to a number of digits from the decimal point. ```python!= print("Number is rounded to two digits: (3.141592, 2) : ", round(3.141592, 2)) print("Number is rounded to four digits: (3.141592, 2) : ", round(3.141592, 4)) ``` <iframe src="https://trinket.io/embed/python3/a512c6ec76?runOption=run&start=result" width="100%" height="150" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> #### String (`str`) String represents a sequence of characters (text) inside double or single quotes. In Python, strings are immutable so once it's declared the value cannot be changed, instead a new object is created. ```python!= first_string = "Hello" second_string = first_string print(first_string) print(second_string) First_string = "I have changed" print(second_string) ``` <iframe src="https://trinket.io/embed/python3/8ed5f2077f?runOption=run&start=result" width="100%" height="160" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> From the above example, we can see the difference between mutating an object and changing a reference. By assigning a value "I have changed" to `first_string` it only changes the reference, while the String object it originally referred to remains unchanged. Also, the string class has a lot of the string useful methods for string manipulation, e.g. `capitalize()`. ```python!= first_name = "John" print(first_name.capitalize()) ``` <iframe src="https://trinket.io/embed/python3/e6bc7aae8a?runOption=run&start=result" width="100%" height="100" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> #### Data type checking with `type()` Python provides a special function called `type()` that tells us the data type (or "class") of any value. Below shows some examples. ```python!= print(type(True)) print(type(3)) print(type(3.14)) print(type("pi")) ``` <iframe src="https://trinket.io/embed/python3/cff49d3873?runOption=run&start=result" width="100%" height="150" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> #### Type conversion with `eval()` We can use `eval()` to perform the type conversion and evaluation of an expression in string. ```python!= ans = eval(input("Enter an expression: ")) print(ans) ``` <iframe src="https://trinket.io/embed/python3/f6419ac606?runOption=run&start=result" width="100%" height="100" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> :::warning **Warning**: It is **NOT suggested** to use the `eval()` function in your program. The `eval()` function is very powerful and _also potentially dangerous_. When we evaluate user input, we are essentially allowing the user to enter a portion of our program. Python will dutifully evaluate whatever they type. Someone who knows Python could exploit this ability to enter malicious instructions. For example, the user could type an expression that captures private information or delete files on the computer. In computer security this is called a _code injection attack_, because an attacker is injecting malicious code into the running program. ::: #### Summary Pythons supports type inference which is a process for automatic detection of the type of the data, so the keywords that are used for creating data types in most of the programming languages (e.g. `int`, `float`, `bool`) are not required in Python. ## Variables Variables are the named memory location with specific data type. The data type determines the values and the operations that may be used. ### Variable declaration and initialization To create a variable, the identifier and its value are required. Below shows the declaration of variables in Python. ![](https://i.imgur.com/am6EJC5.png) To define a variable is just like to tell the others about a box, where the data type is like the shape, colour or volume of the box, and the identifier is like a label that named the box. ![](https://i.imgur.com/f3xPqJq.png) Before using the variable, they are required to _initialize_, i.e. a value should be given before use. It is just like putting a value into a box. ![](https://i.imgur.com/9YJ9CmC.png) ## Standard Input and Output in Python In Python, data in input to and output from a _stream_. A _stream_ is a source of or destination of data. The text stream that interact with users is known as _standard input and output_, while the input text stream is sent from the keyboard, and the output text stream is sent to the monitor. ![](https://i.imgur.com/ElPL2eG.png) Some of the functions like `input()` and `print()` are widely used for standard input and output operations respectively. ### Python Output The `print()` function is used as the standard output function of Python. We use the `print()` function to output data to the standard output device, i.e. screen. It involves the following steps. 1. Take a set of data values 2. Use a _format control string_ that includes the _conversion specification_ and any textual data to be inserted into the text stream 3. Send the result to the standard output device ![](https://i.imgur.com/bOotoqh.png) Below shows a simple example. ```python!= print("Mr Yeung is so handsome.") age = 18 print("He is", age, "years old.") ``` <iframe src="https://trinket.io/embed/python3/2251689091?runOption=run&start=result" width="100%" height="120" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> In the second `print()` statement, we can notice that space was added between the string and the value of variable age. We can change it by modifying the function. The actual syntax of the `print()` function is shown as below. `print(\*objects, sep=" ", end="\n", File=file.sys.stdout, flush=False)` `objects`: the value(s) to be printed `sep`: separator used between the values. The default is a space. `end`: after all values are printed, `end` is printed. The default is a new line character (`\n`). `File`: the object where the values are printed. The default value is `sys.stdout (screen)`. Below shows some examples. ```python!= print(2,1,1,7) print(2,1,1,7, sep="-") print(2,1,1,7, sep="-", end="*") print(5,1,1,7, sep="-") ``` <iframe src="https://trinket.io/embed/python3/b9d72916d2?runOption=run&start=result" width="100%" height="140" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> #### The Python String `.format()` Method (\*) Sometimes we would like to format the output to make it look more attractive. This can be done by using `format()` method. ##### Positional arguments In the following example, the curly braces `{}` are used as placeholders called _positional arguments_. The order can be specified in which they are printed by using numbers. ```python!= a = 10 b = 5 print("The sum of {} and {} is {}.".format(a, b, a + b)) ``` <iframe src="https://trinket.io/embed/python3/34c0b8e08c?runOption=run&start=result" width="100%" height="125" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/A7AHbMK.png) In the following example, numbers are put inside the curly brackets. Each positional arguments is inserted into the template in the palace of its corresponding replacement field. ```python!= print("I love {0} and {1}".format("ICT", "MFS")) print("I love {1} and {0}".format("ICT", "MFS")) ``` <iframe src="https://trinket.io/embed/python3/0314c90756?runOption=run&start=result" width="100%" height="120" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/QtQFNdK.png) ##### Keyword arguments The following example uses _keyword arguments_ instead of positional parameters to produce the result. Each keyword value is inserted into the template in place of its corresponding replacement field. ```python!= print("My name is {name} and I am {age} years old.".format( name = "Mr Yeung", age = 18)) ``` <iframe src="https://trinket.io/embed/python3/ce8553882f?runOption=run&start=result" width="100%" height="140" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ![](https://i.imgur.com/244ZEbA.png) ##### \*More on string formatting Below diagram shows the string formatting to an output. ![](https://i.imgur.com/jGTl4yw.png) ![](https://i.imgur.com/8rspuVN.png) ![](https://i.imgur.com/9XtJ8mM.png) ###### The formatting operator Below shows the formatting operator in `format()`. ![](https://i.imgur.com/Jj3qhol.png) There are several types of conversion code. For example, `i` is for _integers_, `f` is for _floating point_, and `s` is for _string_. | **Type** | **Code** | | --- | --- | | Integer | `d` | | Floating point | `f` | | String | `s` | | Integer in binary | `b` | | Integer in octal | `o` | | Integer in hexadecimal | `x` | ```python!= a = 17 b = 15 c = 0x17 print("The decimal number {0:d} is {0:o} in octal".format(a)) print("The decimal number {0:d} is {0:o} in hexadecimal".format(a)) print("The octal number {0:o} is {0:d} in decimal".format(b)) print("The octal number {0:o} is {0:x} in hexadecimal".format(b)) print("The hexadecimal number {0:x} is {0:o} in octal".format(c)) print("The hexadecimal number {0:x} is {0:d} in decimal".format(c)) ``` <iframe src="https://trinket.io/embed/python3/504156c93d?runOption=run&start=result" width="100%" height="300" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ###### Width modifiers The width modifiers indicate the minimum width (number of characters, including the decimal point (`.`)) of the data. For example, 5d means that the minimum printing position of the integer is 5. If the data required using more space than we allow, say showing 123456 with `5d`, then the `format()` function overrides the width, i.e. showing 123456 instead of 12345 or 23456. If no width modifier is used, then the output value will take just enough room for the data. ###### Precision modifiers The precision modifiers specify the number of decimal places to be printed for floating point numbers in a format of `.m`, where `m` is an integer. If it is not specified, a floating point number is printed with a 6 decimal position. For example, `6.3f` printed the floating point in a format of `XX.XXX` (width is 6 printing positions, where the decimal point (`.`) uses 1 printing position, and also 3 printing positions for the decimal places). ###### Flag modifiers The following table shows that flag type can flag code of the flag modifiers. | **Flag Type** | **Flag Code** | **Formatting** | | --- | --- | --- | | Justification | \< | Left justified | | | \> | Right justified | | | ^ | Center justified | | Padding | None | Space padding | | | 0 | Zero padding | | Sign | None | Positive value: no sign Negative value: - | | | 0 | Positive value: + Negative value: - | | | Space | Positive value: space Negative value: - | Below shows some examples of the flag modifier. | **Expression** | **Result** | | --- | --- | |`print("#{:6d}#".format(12))` | `# 12#` | | `print("#{: - 6d}#".format(12))` | `#12 #` | | `print("#{: 0 6d}#".format(12))` | `#000012#` | | `print("#{: + 6d}#".format(12))` | `# +12#` | | `print("#{: d}#".format(12))` | `# 12#` | | `print("#{: d}#".format(-12))` | `#-12#` | ###### Output modifiers examples Here are some examples of the output modifiers. | **Program** | **Result** | | --- | --- | | `print("{:d}{:s}{:f}".format(23, 'z', 4.1))` | `23z4.1000000` | | `print("{:d} {:s} {:f}".format(23, 'z', 4.1))` | `23 z 4.1000000` | | `num1 = 23zee = "z"num2 = 4.1print("{:d} {:s} {:f}".format(num1, zee, num2))` | `23 z 4.1000000` | | `print("{:d}\t{:s}\t{:5.1f}".format(23, "z", 14.2))`|`23 Z 14.2`| | `print("{:d}\t{:s}\t{:5.1f}".format(107, "A", 53.6))` |`107 A 53.6`| | `print("{:d}\t{:s}\t{:5.1f}".format(1754, "F", 122.0))` |`1754 F 122.0`| | `print("{:d}\t{:s}\t{:5.1f}".format(3, "P", 0.1))` | `3 P 0.1` | | `print("The number is {:6d}.".format(23))` | `The number is 23.` | | `print("The tax is {:6.2f} this year.".format(232.12))` | `The tax is 233.12 this year.` | | `print("The tax is {:8.2f} this year.".format(232.12))` | `The tax is 233.12 this year.` | | `print("The tax is {:08.2f} this year.".format(232.12))` | `The tax is 00233.12 this year.` | | `print("\"{:\>8s} {:d}".format('h', 23))` | `" h 23"` | <iframe src="https://trinket.io/embed/python3/441a875da9?runOption=run&start=result" width="100%" height="320" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ##### Further studies For more details, you can refer to the following website. [https://realpython.com/python-formatted-output/](https://realpython.com/python-formatted-output/) ### Python Input The purpose of an input statement is to get some information from the user of a program. #### Good programming practice If the target variable is not a string, we usually use the type conversion functions immediately with the `print()` function to keep the data type. ```python!= x = float(input("Enter a number between 0 and 1: ")) celsius = int(input("What is the Celsius temperature? ")) ``` <iframe src="https://trinket.io/embed/python3/50aa46ca79?runOption=run&start=result" width="100%" height="120" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> ## Chapter Summary - The way a computer represents a particular kind of information is called a data type. - Python has several different data types for representing numeric values, including int and float. - Identifiers are names; they begin with an underscore or letter which can be followed by a combination of letter, digit, or underscore characters. Identifiers in Python are case-sensitive. - Strings are sequences of characters. String literals can be delimited with either single or double quotes. - Program input and output often involve string processing. Python provided numerous operators for converting back and forth between number and strings. - The string formatting method (`format`) is particularly useful for producing naively formatted output. ## Exercise 1. Python identifiers must start with a letter or underscore. A. True B. False 2. Keywords make good variables names A. True B. False 3. The float data type is identical to the mathematical concept of a real number. A. True B. False 4. In Python, `4 + 5` produces the same result type as `4.0 + 5.0` A. True B. False 5. In Python, `"4" + "5"` is `"45"`. A. True B. False 6. An expression that evaluates to either `True` or `False` is called A. operational B. Boolean C. simple D. compound 7. Which of the following is _not_ a built-in Python data type? A. int B. float C. rational D. String 8. The most appropriate data type for storing the value of pi is A. int B. float C. string D. Boolean ## Programming exercise ### Exercise 1 Write a program to find the average of three exam scores. A sample run is shown below. ``` Exam score of first exam: 90 Exam score of second exam: 80 Exam score of third exam: 70 The average exam score is 80 ``` ### Exercise 2 Write a program that converts temperatures from Fahrenheit to Celsius (round to 2 decimal places). Given that Fahrenheit = $\frac{9}{5} \times \text{celsius} + 32$. A sample run is shown below. ``` Enter the temperatures from Fahrenheit: 100 100 Fahrenheit equals 37.78 Celsius. ``` ### Exercise 3 Write a program that converts distances measured in kilometers to miles. One kilometer is approximately 0.62 miles. ### Exercise 4 The Konditorei coffee shop sells coffee at $10.50 a pound plus the cost of shipping. Each order ships for $0.86 per pound + $1.50 fixed cost for overhead. Write a program that calculates the cost of an order ### Exercise 5 Two points in a plane are specified using the coordinates (x1, y1) and (x2, y2). Write a program that calculates the slope of a line through two (non-vertical) points entered by the user. ### Exercise 6 Write a sales report printing program for the ABC Food Company. The company has four products: "Beverages", "Meat", "Produce" and "Seafood". Ask the user to enter the quantity (an integer) and the unit price (a double) for each product. The program calculates the amount by multiplying the quantity and the unit price for each product, and the total amount of all products. Finally the program prints out a report as follows. You need to follow the format and the spacing of the report, e.g., item names are aligned on the left and the numbers are aligned on the right. (The data underlined is entered by use ``` Enter the quantity for "Beverages": 925 Enter the unit price for "Beverages": 9.5 Enter the quantity for "Meat": 92 Enter the unit price for "Meat": 75.35 Enter the quantity for "Produce": 351 Enter the unit price for "Produce": 42.25 Enter the quantity for "Seafood": 669 Enter the unit price for "Seafood": 113.40 ================================================ ABC Food Company Monthly Sales Report ------------------------------------------------ Item Name Quantity Unit Price Amount ------------------------------------------------ Beverages 925 9.50 8787.50 Meat 92 75.35 6932.20 Produce 351 42.25 14829.75 Seafood 669 113.40 75864.60 ------------------------------------------------ Total 106414.05 ================================================ ``` ## Appendix 1 - Reserved word in Python `False`, `None`, `True`, `and`, `as`, `assert`, `break`, `class`, `continue`, `def`, `del`, `elif`, `else`, `except`, `finally`, `for`, `from`, `global`, `if`, `import`, `in`, `is`, `lambda`, `nonlocal`, `not`, `or`, `pass`, `raise`, `return`, `try`, `while`, `with`, `yield`