VJC Chapter 2 Data Types
Uploaded by cheesemuffin · 10 December 2025
Preview
Text from the first pages2022/VJC/JC1/H2Computing/9569 Chapter 2: Data Types and Operators Contents 1 Data Types 1.1 Type conversion 1.2 Input 1.3 Output 2 Numbers 2.1 Integer in different bases 2.2 Arithmetic Operators 3 Boolean 3.1 Boolean operators 3.2 Logical operators 4 Strings 4.1 ASCII code and Unicode 4.2 Create Strings 4.3 Escape character 4.4 Indexing 4.5 Slicing 4.6 Immutable 4.7 String operators 4.8 Built-in String methods Syllabus Learning Outcomes 2.2 Programming Elements and Constructs Use programming language elements and constructs to write recursive and non-recursive programs to solve a variety of problems. 2.2.1 Understand the different types: integer, real, char, string and Boolean and initialise arrays (1- dimensional and 2-dimensional). 2.2.2 Use common library functions for input/output, strings and mathematical operations. 3.1 Data Representation Understand that values can be represented in different number bases: denary, binary and hexadecimal. 3.1.1 Represent data in binary and hexadecimal forms. 3.1.2 Write programs to perform the conversion of positive integers between different number bases: denary, binary and hexadecimal forms; and display results. 3.2 Character Encoding Understand the use of ASCII code and Unicode to represent characters. 3.2.1 Give examples of where or how Unicode is used. 3.2.2 Use ASCII code in programs. 1
2022/VJC/JC1/H2Computing/9569 1 Data Types Multiple data types are developed for convenient data manipulation and management. In the table below are some of the more common data types: The data type is set when you assign a value to a variable. The type() function can be used to check the data type of a variable. 1.1 Type conversion We can convert one type of number into another. Operations like addition, subtraction can convert integer to float implicitly (automatically), if one of the operands is float. We can also use built-in functions like int() , float() and str() to convert between types explicitly. 2 Data Types Description Example Integer A signed whole number 1, 1234, -15 Float (real) A signed number with a decimal point 1.0, 2.345, - 54.321 Boolean The logical values TRUE and FALSE TRUE, FALSE String A sequence of zero or more characters ‘hello’, ‘action 123’, ‘A-level’, ‘’
2022/VJC/JC1/H2Computing/9569 1.2 Input We can ask user for input using Python built-in input() function. ● When input() function is called, the program will stop and wait for user to key in data. ● It is optional to add prompt as function parameter. ● It always returns a string value. Notice that even when the value entered by user is an integer, the returned value will still be of the str type. We need to convert the type of the returned value using the built-in functions for further manipulation of the returned value if required. 3
2022/VJC/JC1/H2Computing/9569 1.3 Output Python provides a built-in function print() to display data to the console or a file object. The print() function ● can accept multiple values, and uses sep value to separate them in output. By default, the sep value is a space, ● automatically appends an end string at the end. By default, the end value is a new line (\n), and ● outputs data to a file object defined in file, which has a default value sys.stdout (console). 4
2022/VJC/JC1/H2Computing/9569 2 Numbers 2.1 Integer in different bases The numbers we deal with every day are of the decimal (base 10) number system. However, computer programmers (generally embedded programmers) need to work with binary (base 2), hexadecimal (base 16) and octal (base 8) number systems. In Python, we can represent these numbers by appropriately placing a prefix before that number. The following table lists these prefixes. Number System Prefix Binary '0b' or '0B' Octal '0o' or '0O' Hexadecimal '0x' or '0X' We can use bin() and hex() to convert a decimal number to a binary and hexadecimal respectively. We can also use int() to convert other number systems to decimals. 5
2022/VJC/JC1/H2Computing/9569 2.2 Arithmetic Operators Arithmetic operators perform common mathematical operations on values. 6 Operator Name Expression Example Answer + Addition x + y 2+3 5 - Subtraction x - y 7-3 4 * Multiplication x * y 2*3 6 / Division x / y 20/8 2.5 // Integer division x // y 20//8 2 % Modulus x % y 20%8 4 ** Exponentiation x ** y 4**2 16
2022/VJC/JC1/H2Computing/9569 Sometimes we want to do more than just usual arithmetic operation. Python has a built-in function that gives us access to more functions. (Refer to annex on how to import a module) 3 Boolean A Boolean variable can represent either True or False . 3.1 Boolean operators Comparison operators are used to compare two values. It returns a boolean value. Operator Name Example > Greater than x > y < Less than x < y >= Greater than or equal to x >= y <= Less than or equal to x <= y == Equal x == y != Not equal x != y 7
2022/VJC/JC1/H2Computing/9569 An object can also be evaluated to be True or False using built-in bool() function. 3.2 Logical Operators Sometimes we need to make decisions based on multiple conditions. Logical operators are used to combine conditional statements. ● Operands shall be conditions which can result in a boolean value. ● The outcome of such an operation is either true or false too. Operator Description Example and Returns True if both statements are true x < 5 and x < 10 or Returns True if at least one of the statements is true x < 5 or x < 10 not Reverse the result, returns False if the result is true not(x < 5 and x < 10) Given that x = 6, predict the outcome in the "Example" column from the table above. 8
2022/VJC/JC1/H2Computing/9569 4 Strings Characters in programming are not limited to the 26 English characters. It can also be space character, question mark, Chinese character, etc. A string is a sequence of characters. It can be a word, a sentence or a paragraph. 4.1 ASCII code and Unicode Today’s programs need to be able to handle a wide variety of characters. Applications are often internationalized to display messages and output in a variety of user-selectable languages; the same program might need to output an error message in English, French, Japanese, Hebrew, or Russian. Web content can be written in any of these languages and can also include a variety of emoji symbols. Two standard character sets used in displaying text are ASCII code and Unicode. ASCII stands for American Standard Code for Information Interchange The ASCII character set is a 7-bit set of codes that allows 128
Content continues in the PDF. Download PDF
Related notes
- NYJC 2026 Prelim P2Exam Papers · 2026
- NYJC 2026 Prelim P1Exam Papers · 2026
- DHS 2026 Y6 H2 Computing Prelim Paper 2_finalExam Papers · 2026
- ACJC 2026 JC2 Computing Prelim Paper 2 (Practical)Exam Papers · 2026
- 2026_NJC Prelim_Computing_P2.pdfExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P2_finalExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_markschemeExam Papers · 2026
- 2026_JPJC_Computing_Prelim_P1_finalExam Papers · 2026
- 2026 ACJC Prelim Computing Paper 2Exam Papers · 2026
- 2024 ACJC Computing PromoExam Papers · 2024
- 2023 ACJC Promo QPExam Papers · 2023
- 2022 ACJC Computing Promo Paper 2Exam Papers · 2022
- See all H2 Computing notes

