0% found this document useful (0 votes)
14 views22 pages

Introduction to Computer Programming Basics

The document provides an introduction to computer programming, covering the definition of computers, their characteristics, and the architecture of a computer, including the CPU and memory types. It explains programming as the process of writing instructions for computers and discusses machine language and binary representation. Additionally, it highlights the importance of input and output devices and the role of programming languages in instructing computers to perform tasks.

Uploaded by

horace23vt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views22 pages

Introduction to Computer Programming Basics

The document provides an introduction to computer programming, covering the definition of computers, their characteristics, and the architecture of a computer, including the CPU and memory types. It explains programming as the process of writing instructions for computers and discusses machine language and binary representation. Additionally, it highlights the importance of input and output devices and the role of programming languages in instructing computers to perform tasks.

Uploaded by

horace23vt
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2025/1/3 15:13 Introduction to Computer Programming

Introduction to Computer
Programming
In [1]: # Set LLM alias
%load_ext jupyter_ai
%ai update chatgpt dive:chat

Out[1]: Updated target of alias chatgpt

CS1302 Introduction to Computer Programming

What is computer
What is programming
Python programming language

Computer

What is a computer?
A calculator that is bigger and more advanced?

A calculator on a computer.

If so, is abacus the first computer invented?

[Link] Introduction/Introduction to Computer [Link] 1/22


2025/1/3 15:13 Introduction to Computer Programming

Abacus - an ancient mechanical computing device.

Is your smartphone a computer?


What defines a computer?

{important}

A computer is an electronic device that manipulates information


or data, and performs arithmetic calculations.

In addition to performing arithmetic calculations, a computer


can be *programmed* to perform different tasks.

In [2]: %%ai chatgpt -f text

What are the characteristics of a computer?

[Link] Introduction/Introduction to Computer [Link] 2/22


2025/1/3 15:13 Introduction to Computer Programming

Out[2]: The characteristics of a computer include:

1. **Speed**: Computers can perform calculations and process information at ext


remely high speeds, much faster than humans.
2. **Accuracy**: Computers can perform tasks with a high degree of accuracy, as
long as the input is correct and the software is functioning properly.
3. **Reliability**: Computers can perform tasks repeatedly and consistently, wi
thout getting tired or making mistakes.
4. **Versatility**: Computers can be used for a wide range of tasks, from simpl
e calculations to complex simulations, and can be programmed to perform many di
fferent functions.
5. **Memory**: Computers have the ability to store and retrieve large amounts o
f data, allowing them to perform tasks that require access to large amounts of
information.
6. **Input/Output**: Computers can accept input from users and provide output i
n various forms, such as text, images, and sound.
7. **Automatic**: Computers can perform tasks automatically, without the need f
or human intervention, once they have been programmed to do so.
8. **Digital**: Computers process information in digital form, using binary cod
e (0s and 1s) to represent data and instructions.
9. **Interactivity**: Computers can interact with users, allowing them to input
data, receive feedback, and adjust the computer's behavior accordingly.
10. **Storage**: Computers have the ability to store data and programs, allowin
g them to retain information even when they are turned off.
11. **Networking**: Many computers can connect to other computers and devices,
allowing them to share information, communicate with other users, and access re
mote resources.
12. **Scalability**: Computers can be upgraded or expanded to increase their pr
ocessing power, memory, and storage capacity, allowing them to handle more comp
lex tasks and larger amounts of data.
13. **Portability**: Many computers are designed to be portable, allowing them
to be easily moved and used in different locations.
14. **Cost-effectiveness**: Computers can perform many tasks more quickly and a
ccurately than humans, making them a cost-effective solution for many applicati
ons.

Overall, these characteristics make computers powerful tools for processing inf
ormation, automating tasks, and communicating with others.

What is the architecture of a computer?


Almost all computers are based on Von Neumann architecture, which consists of
the following three main components:

1. Input/output device: read information into or out of the memory unit upon
command from the CPU
2. Central Processing Unit (CPU)
Arithmetic and Logic Unit (ALU): performs arithmetics like a calculator
(but for binary numbers)
Control Unit (CU): directs the operations of the processor in executing a
program.
3. Main memory: stores both data and instructions

[Link] Introduction/Introduction to Computer [Link] 3/22


2025/1/3 15:13 Introduction to Computer Programming

Computer architecture.

Input and output devices

Computer Peripherals.

Input and output devices connected to a computer are called peripherals.


They allow users to interact with the computer in different ways.

::::{exercise} :label: ex:output-device

Some examples of output devices are:

Monitor
Speaker

Can you give an awesome example in the following box? ::::

[Link] Introduction/Introduction to Computer [Link] 4/22


2025/1/3 15:13 Introduction to Computer Programming

YOUR ANSWER HERE

In [4]: %%ai chatgpt -f text

Please give one example of output devices of a computer besides monitor and spea

Out[4]: One example of an output device besides monitor and speaker is a **Printer**. A
printer is an output device that produces physical copies of text, images, and
other data on paper or other materials.

Monitor and speaker are examples of output devices because they provide output
from the computer to the user, allowing them to perceive and interact with the
information being presented. The **monitor** displays visual output, such as te
xt and images, while the **speaker** produces audio output, such as sound and m
usic.

::::{exercise} :label: ex:input-device

Some examples of input devices are:

Keyboard
Mouse

Can you give an awesome example? ::::

YOUR ANSWER HERE

In [5]: %%ai chatgpt -f text

Please give one example of input devices of a computer besides keyboard and mous

Out[5]: One example of an input device besides keyboard and mouse is a **Scanner**. A s
canner is an input device that captures images or text from physical documents
and sends them to the computer for editing, storage, or other processing.

Keyboard and mouse are examples of input devices because they allow users to en
ter data and control the computer. The **keyboard** enables users to type text
and enter commands, while the **mouse** allows users to navigate and interact w
ith the computer's graphical user interface by clicking, dragging, and scrollin
g.

::::{exercise} :label: ex:input-output

Many devices are both input and output device. Can you give at least 3 examples?
::::

YOUR ANSWER HERE

In [6]: %%ai chatgpt -f text

Please provide 3 examples of device that is both an input device and an output d

[Link] Introduction/Introduction to Computer [Link] 5/22


2025/1/3 15:13 Introduction to Computer Programming

Out[6]: Here are 3 examples of devices that can be both input and output devices:

1. **Touchscreen**: A touchscreen can accept input from the user's touches and
gestures, and at the same time, display output from the computer, such as image
s, text, and videos.
2. **Tablet**: A tablet can accept input from the user's stylus or finger, and
simultaneously display output, such as graphics, text, and videos, on its scree
n.
3. **Smartphone**: A smartphone can accept input from the user's touches, voice
commands, and keystrokes, and at the same time, display output, such as text me
ssages, emails, and videos, on its screen.

These devices are often referred to as "interactive" devices, as they allow use
rs to interact with the computer or other digital systems in a two-way manner.

Input and output devices.

Central Processing Unit

An Intel CPU.

The brain of a computer is its processor unit, or the Central Procesisng Unit (CPU).
It is located on the motherboard and connects to different peripherals using
different connectors.

Two important components in the CPU are:

[Link] Introduction/Introduction to Computer [Link] 6/22


2025/1/3 15:13 Introduction to Computer Programming

Arithmetic and Logic Unit (ALU): Performs arithmetics like a calculator (but
for binary numbers)
Control Unit (CU): Directs the operations of the processor in executing a
program.

To visualize how CPU works, run the CPU Simulator below.

Note that all values are zeros in the RAM (Random Acess Memory) initially.
Under Settings, click Examples $\to $ Add two numbers . Observe that the
values in the RAM have changed.
Click Run at the bottom right-hand corner.

:::::{card} :footer: Open in new tab [Link]

::::{iframe} [Link] :width: 100% ::::

:::::

If the link above does not work, you can try Open in new tab

Memories

Computer memory is the storage space in the computer, where data is to be


processed and instructions required for processing are stored.

Computer memory is of two basic types:

Random Access Memory (RAM):


It is also called the main memory or the primary memory.
The programs and data that the CPU requires during the execution of a
program are stored in this memory.
It is a volatile memory as the data lost when the power is turned off.
RAM is further classified into two types- SRAM and DRAM.
Read Only Memory (ROM)
Stores crucial information essential to operate the system, like the
program essential to boot the computer.
It is not volatile.
Always retains its data.
ROM is further classified into three types- PROM, EPROM, and EEPROM.

[Link] Introduction/Introduction to Computer [Link] 7/22


2025/1/3 15:13 Introduction to Computer Programming

Programming

What is programming?

{important}

*Programming*, also called *software development*, is the


process of writing programs for the computer to execute
different tasks.

::::{exercise} :label: ex:machine-language

You have just seen a program written in machine language. Where?

::::

YOUR ANSWER HERE

In [7]: %%ai chatgpt


Summarize what a machine language is and give an example of a program in machine

[Link] Introduction/Introduction to Computer [Link] 8/22


2025/1/3 15:13 Introduction to Computer Programming

Out[7]:
Machine Language
Machine language, also known as machine code, is the lowest-level programming
language that a computer's processor can execute directly. It consists of binary
code, which is a series of 0s and 1s that represent instructions and data.

Example of a Program in Machine Language


A simple example of a program in machine language that adds two numbers
might look like this:

10101010 11010011 00000001


11010101 10111011 00000010
10010010 01110101 00000111
This program consists of a series of binary instructions that the computer's
processor can execute directly. Note that machine language is specific to the
computer's architecture and is not portable across different machines.

Programming is a way to “instruct the computer to perform various tasks”.

“Instruct the computer”: this basically means that you provide the computer a
set of instructions that are written in a language that the computer can
understand. The instructions could be of various types. For example:

Adding 2 numbers,
Rounding off a number, etc.
“Perform various tasks”: the tasks could be simple ones like we discussed
above (adding 2 numbers, rounding off a number) or complex ones which
may involve a sequence of multiple instructions. For example:

Calculating simple interest, given principal, rate and time.


Calculating the average return on a stock over the last 5 years.

Hence, in summary, Programming is a way to tell computers to do a specific task.

What is programming language?

Just like we humans can understand a few languages (English, Spanish, Mandarin,
French, etc.), computers understand instructions that are written in a specific
syntactical form called a programming language.

How code and data is stored in computer?


All computer data is represented using binary, a number system that uses 0s and
1s.

Both the instructions and the numbers are represented as binary sequences.

[Link] Introduction/Introduction to Computer [Link] 9/22


2025/1/3 15:13 Introduction to Computer Programming

E.g., in Intel-based CPU, the command for addition is 00000011 00000100

Binary representation.

Why computer uses binary representation?


In [8]: %%html
<iframe width="800" height="450" src="[Link]

An error occurred.

Try watching this video on [Link], or enable JavaScript if it is disabled in your browser

::::{exercise} :label: ex:ENIAC

The first electronic computer, called Electronic Numerical Integrator and


Computer (ENIAC), was programmed using binary circuitries, namely switches that

[Link] Introduction/Introduction to Computer [Link] 10/22


2025/1/3 15:13 Introduction to Computer Programming

can be either On or Off .

:::{figure}
[Link]
:name: fig:ENIAC :alt: Two programmers operating ENIAC. :align: left

Programmers controlling the switches of ENIAC. :::

However, it did not represent values efficiently in binary. 10 binary digits (bits) was
used to represent a decimal number 0 to 9.
Indeed, how many decimals can be represented by 10 bits?

::::

In [9]: %%ai chatgpt


Please provide a piece of Python code to print the total number of values repres

Out[9]:
Calculating Total Number of Values Represented by 10
Bits
The total number of values that can be represented by 10 bits is 2^10, since each
bit can have 2 possible values (0 or 1).

Python Code
# Calculate the total number of values represented by 10 bits
total_values = 2 ** 10

# Print the result


print("The total number of values represented by 10 bits is:", total_values)
When you run this code, it will output:

The total number of values represented by 10 bits is: 1024

In [10]: # Calculate the total number of values represented by 10 bits


total_values = 2 ** 10

# Print the result


print("The total number of values represented by 10 bits is:", total_values)

The total number of values represented by 10 bits is: 1024

As mentioned in the video, there are International Standards for representing


characters:

ASCII (American Standard Code for Information Interchange) maps English


letters and some other symbols to 8-bits (8 binary digits, also called a byte).
E.g., A is 01000001 .

[Link] Introduction/Introduction to Computer [Link] 11/22


2025/1/3 15:13 Introduction to Computer Programming

ASCII Table.

Unicode can also represent characters in different languages such as Chinese,


Japanese...etc.

Unicode.

There are additional standards to represent numbers other than non-negative


integers:

2's complement format for negative integers (e.g. -123)


IEEE floating point format for floating point numbers such as $1.23 x
10^{-4}$.

{note}
**Why define different standards?**

[Link] Introduction/Introduction to Computer [Link] 12/22


2025/1/3 15:13 Introduction to Computer Programming

- Different standards have different benefits. ASCII requires


less storage for a character, but it represents less characters.
- Although digits are also represented in ASCII, the 2's
complement format is designed for more efficient arithmetic
operations.

Different generations of programming


languages

{important}

A *machine language* written in *binary sequences* is known as a


*1st Generation Programming Language*.

Machine language.

Are we going to start with machine language? Start with learning 2's
complement and the binary codes for different instructions?

No. Programmers do not write machine codes directly because it is too hard to
think in binary representations.

Instead, programmers write human-readable mnemonics such as ADD , SUB , ...


This is called the assembly language:

[Link] Introduction/Introduction to Computer [Link] 13/22


2025/1/3 15:13 Introduction to Computer Programming

A Code written in an assembly language.

{important}

An *assembly language* written in *mnemonics* is the *2nd


Generation Programming Language*.

Are you going to learn an assembly language?

Both machine language and assembly language are low-level languages which
are

[Link] Introduction/Introduction to Computer [Link] 14/22


2025/1/3 15:13 Introduction to Computer Programming

difficult to write for complicated tasks (requiring many lines of code), and
platform-specific:
the sets of instructions and their binary codes can be different for
different types of CPUs, and
different operating systems use different assembly languages/styles.

Anyone wants to learn assembly languages, and write a program in many versions
to support different platforms?

Probably for programmers who need to write fast or energy-efficient code such as

a driver that controls a 3D graphics card, and


a program that control a microprocessor with limited power supply.

But even in the above cases, there are often better alternatives. Play with the
following microprocessor simulator:

Click CHOOSE A DEMO $\to $ LED .


Click RUN SCRIPT and observes the LED of the board.
Run the demos ASSEMBLY and MATH respectively and compare their
capabilities.

In [11]: %%html
<iframe width="810", height="1000" src="[Link]

[Link] Introduction/Introduction to Computer [Link] 15/22


2025/1/3 15:13 Introduction to Computer Programming

[Link] Introduction/Introduction to Computer [Link] 16/22


2025/1/3 15:13 Introduction to Computer Programming

High-level Language
In [12]: %%html
<iframe width="800" height="450" src="[Link]

An error occurred.

Try watching this video on [Link], or enable JavaScript if it is disabled in your browser

Programmer nowadays write in human-readable languages such as

C/C++
Java
JavaScript/Typescript
Pascal
Basic
HTML
PHP
Python
...

called high-level languages.

What is a high-level language?

{important}

[Link] Introduction/Introduction to Computer [Link] 17/22


2025/1/3 15:13 Introduction to Computer Programming

A *high-level language* intended to be *human understandable* is


a *3rd Generation Programming Language*.

A program written in a high-level language gets converted automatically to a


low-level machine code for the desired platform.
This abstracts away low-level details that can be handled by the computer
automatically.

For instance, a programmer needs not care about where a value should be
physically stored if the computer can find a free location automatically to store
the value.

Different high-level languages can have different implementations of the


conversion processes:

Compilation means converting a program well before executing of the


program.
E.g., C++ and Java programs are compiled.
Interpretation means converting a program on-the-fly during the execution
of a program.
E.g., JavaScript and Python programs are often interpreted.

{seealso}

Roughly speaking, compiled programs run faster but interpreted


programs are more flexible and can be modified at run time. The
[truth]([Link]
programming-languages/) is indeed more complicated.

What programming language will you learn?


You will learn to program in python, covering

[Link] Introduction/Introduction to Computer [Link] 18/22


2025/1/3 15:13 Introduction to Computer Programming

basic topics including values, variables, conditional, iterations, functions,


composite data types,
advanced topics that touch on functional and object-oriented programming,
and
engineering topics such as numerical methods, optimizations, and machine
learning.

Why Python?

In [13]: %%html
<iframe width="800" height="450" src="[Link]

An error occurred.

Try watching this video on [Link], or enable JavaScript if it is disabled in your browser

{note}

Python is:
- *expressive* and can get things done with fewer lines of code
as compared to other languages.
- *popular*. It has an extensive set of libraries for
Mathematics, graphics, AI, Machine Learning, etc.
- *free and open-source*, so you get to see everything and use
it without restrictions.
- *portable*, i.e., the same code runs in different platforms
without modifications.

How does a Python program look like?

[Link] Introduction/Introduction to Computer [Link] 19/22


2025/1/3 15:13 Introduction to Computer Programming

In [14]: import datetime # library to obtain current year

cohort = input("In which year did you join CityU? [e.g., 2020]")
year = [Link]().year - int(cohort) + 1
print("So you are a year", year, "student.")

So you are a year 2 student.

A Python program contains statements just like sentences in natural languages.


E.g.,

cohort = input("In which year did you join CityU? ")


which obtains some information from user input.

For the purpose of computations, a statement often contains expressions that


evaluate to certain values. E.g.,

input("In which year did you join CityU? ")


is an expression with the value equal to what you input to the prompt.
That value is then given the name cohort .

Expressions can be composed of the following objects:

Functions such as input , now , and int , etc., which are like math functions
the return some values based on its arguments, if any.
Literals such as the string "In which year did you join CityU? " and the
integer 1 . They are values you type out literally.
Variables such as cohort and year , which are meaningful names to values.

To help others understand the code, there are also comments that start with # .
These are meant to explain the code to human but not to be executed by the
computer.

::::{exercise} :label: ex:future-programming

What do you think the next generation programming should be? ::::

YOUR ANSWER HERE

In [15]: %%ai chatgpt


What do you think the next generation programming should be?

[Link] Introduction/Introduction to Computer [Link] 20/22


2025/1/3 15:13 Introduction to Computer Programming

Out[15]:
Next Generation Programming
The next generation of programming is likely to be shaped by emerging
technologies and trends. Some potential features of next-generation programming
include:

1. Increased Focus on Artificial Intelligence (AI) and Machine


Learning (ML)
Programmers will need to work closely with AI and ML algorithms to develop
intelligent systems that can learn and adapt to new situations.

2. Greater Emphasis on Cloud Computing and Distributed


Systems
As more applications move to the cloud, programmers will need to develop skills
in designing and implementing scalable, secure, and efficient distributed systems.

3. More Attention to Cybersecurity and Data Protection


With the increasing number of cyber threats, next-generation programmers will
need to prioritize security and data protection in their designs and
implementations.

4. Growing Importance of Internet of Things (IoT) and Edge


Computing
As IoT devices become ubiquitous, programmers will need to develop skills in
working with edge computing, real-time data processing, and device
management.

5. Rise of Quantum Computing and New Programming


Paradigms
The emergence of quantum computing may require programmers to learn new
programming paradigms and models that can take advantage of quantum
computing's unique capabilities.

6. Increased Use of Visual and Interactive Programming Tools


Next-generation programming may involve more visual and interactive tools, such
as graphical programming languages, to make programming more accessible and
intuitive.

7. Greater Collaboration Between Humans and Machines


The next generation of programming may involve more collaboration between
humans and machines, with programmers working closely with AI-powered tools
to develop more efficient and effective software systems.

These are just a few potential features of next-generation programming. As


technology continues to evolve, it's likely that new trends and innovations will

[Link] Introduction/Introduction to Computer [Link] 21/22


2025/1/3 15:13 Introduction to Computer Programming

emerge, shaping the future of programming and software development.

In [16]: %%html
<iframe width="800" height="450" src="[Link]

An error occurred.

Try watching this video on [Link], or enable JavaScript if it is disabled in your browser

[Link] Introduction/Introduction to Computer [Link] 22/22

You might also like