HCI 2. Getting Started with Python
Uploaded by adrianwang2003 · 28 May 2024
Preview
Text from the first pagesHwa Chong Institution H2 Computing 1 2 Getting Started with Python Learning Outcome Python is a high -level, general -purpose programming language for solving problems on modern computer systems. The official online resources can be found at www.python.org, and its popularity has generated many websites and YouTube channels for self-learning. Three tools that we can use to write a program and test it. Python Shell After downloading Python, launch the IDLE to open a Python shell . Shell is useful for experimenting with short expressions or statements to learn new features of the language. Python File When we need to construct complex programs and test them, we can create and save files in program libraries to reuse or share with others. Open a New File in Shell and save the program files use .py extention. Coding Standards Use indentation and white space Use naming conventions (e.g. meaningful identifier names) Write comments (name of programmer, date written, program description and version book-keeping/control) Programming Elements and Constructs Understand the different data types: integer, real, char, string and Boolean Use common library functions for input/output, strings and mathematical operations Character Encoding Give examples of where and how Unicode is used Use ASCII code in programs
Hwa Chong Institution H2 Computing 2 We can then run Python program files within IDLE using menu option F5 for Windows. Here is a sample program for your first experiment. Then we run the file in Python Shell. Running Jupyter Notebook on the school laptop Look for Computing Software shortcut (shown below) on the desktop. Double click on the shortcut to open Windows Explorer. In Windows Explorer, a list of shortcuts is listed. Double click on the Jupyter Notebook shortcut to launch the application.
Hwa Chong Institution H2 Computing 3 A console windows will be launch prior to the browser. Do not close the console windows. Take note that the console windows should remain open for Jupyter Notebook to work. Jupyter notebook home page will be shown in the browser. By default, the home page will display the files on the desktop. Jupyter Notebook File These files are saved in .ipynb extension. Click ‘New’ to create a new notebook in Python 3. Type the codes and run it by clicking the ‘Run’ button, or pressing ‘Ctrl’ + ‘Enter’. Here is the output. The convenience of using Jupyter Notebook is that we can write multiple programs in one file, by clicking the ‘+’ button.
Hwa Chong Institution H2 Computing 4 2.1 Comments and Docstrings Programs should be easily understood and modifiable by any users, so we need to insert comments to explain how we solve the problem and sometimes the meaning of the variable names we defined. Docstring for a paragraph of comments: For short explanation, we use end-of-line comment with #: 2.2 Input and Output Programs usually accept inputs from a source, process them, and output results to a destination. In terminal-based interactive programs like Python, these are the keyboard and terminal display, respectively. To get input from keyboard, we use: <variable identifier> = input (<a string prompt>) To generate the output, we use: print(<expression>) Question: why is it different for name and print(name)?
Hwa Chong Institution H2 Computing 5 2.3 Data Types In real life, we take many things as data, for example, integers, decimal numbers, characters, words, paragraphs. However, in programming, we need to define and categorize them properly. A data type consists of a set of values, a set of operations that can be performed on the values and the way values can be stored on computers. Here we learn the Python way of defining data types though other programming languages may have different names and categories. 2.3.1 Integers In real life, the range of integers is infinite. Computer’s memory places a limit on largest magnitude of integers Python’s int typical range is –231 to 231 – 1 2.3.2 Floating-Point Numbers Python uses floating-point numbers to represent real numbers Python’s float typical range: –10308 to 10308 Typical precision: 16 digits 2.3.3 Characters and Strings Characters in programming is 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. Text processing is by far the most common application of computing. E-mail, text messaging, Web pages, and word processing all rely on and manipulate data consisting of strings of characters. But computer only reads zeros and ones, so character sets are invented to use numbers to represent English characters and notations we use. Unicode set and ASCII set are common character sets in use. A bit is a binary digit, it can be a 1 or 0. So if we use seven bits, we may have 27= 128 possible binary representation of numbers 0 - 127. The original ASCII used seven bits to encode 128 different characters, as shown below.
Hwa Chong Institution H2 Computing 6 In Python, we use ord and chr to convert characters to and from ASCII. There is surely no need to memorize the table but we might use it to traverse within all the alphabets. For example, chr(ord('A')+5) gives ‘F’. ASCII was further developed to eight bits to accommodate more c haracters. However, using more digits takes more memory space as well. Unicode was developed to use a variable bit encoding program and Unicode standard defines UTF-8, UTF -16 and UTF -32. The large capacity of Unicode makes many non -English languages available, e.g. Chinese and Japanese. Furthermore, ASCII is compatible with Unicode. 2.3.4 Boolean Boolean data type consists of two values: True and False 2.4 Literal TYPE OF DATA PYTHON TYPE NAME EXAMPLE LITERALS Integers int -1, 0, 1, 2, 3420000556008 Real numbers float -0.55, .333, 3.14, 6.0 Character strings str “Hi”, “ “, ‘A’, ’66’
Hwa Chong Institution H2 Computing 7 A literal is the way a value of a data type looks to a programmer. In Python, a string literal is a sequence of characters enclosed in single or double quotation marks '' and "" represent the empty string Use ''' and """ for multi-line paragraphs 2.5 Escape Sequences 2.6 Variables and the Assignment Statement A variable associates a name with a value to make it easy to remember and use later. Variable naming rules: – Reserved words cannot be used as variable names. Examples: if, def, import – Name must begin with a letter or _ – Name can contain any number of letters, digits, or _ – Names are case sensitive. Example: WEIGHT is different from weight – All uppercase letters for symbolic constants. Examples: TAX_RATE – “camel casing”. Example: InterestRate – Short names are preferred. If necessary, we can write comments to explain short forms. Variables receive initial values, and can be reset to new values with an assignment statement. <variable name> = <expression> Subsequent uses of the variable name in expressions are known as variable references. Press ‘Enter’ here to get to next line
Hwa Chong Institution H2 Computing 8 2.7 Expressions A literal evaluates to itself A variable reference evaluates to the variable’s current value Expressions provide easy way to perform operations on data values to produce other values When entered at Python shell prompt, expression’s operands are evaluated and its operator is then applied to these values to compute the value of the expression 2.7.1 Arithmetic Expressions An arithmetic expression consists of operands and operators combined in
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

