1 — Programming Basics
Binary Numbers
In decimal system, a number is expressed as a sequence of digits 0 to
9. For example, Two thousand twenty one ⇔ 2021
In binary number system the set of digits, is called binary digits or
bits: {0, 1}.
A binary number is expressed as a sequence of bits.
For example, 183 in binary is 10110111.
Converting from decimal to binary
Enter decimal number: 123
Quotient Remainder
123 / 2 61 1
61 / 2 30 1
30 / 2 15 0
15 / 2 7 1
7/2 3 1
3/2 1 1
1/2 0 1
Collecting remainders from bottom to top, 123 in binary is 1111011
Converting from binary to decimal
Enter binary number: 1011
= 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20
= 1×8 + 0×4 + 1×2 + 1×1
= 8 + 0 + 2 + 1
= 11
Groups of bits
A group of 8 bits is called a byte e.g. 11010111
1 kilobyte (kB) = 1000 bytes
1 megabyte (MB) = 106 (million) bytes
1 gigabyte (GB) = 109 (billion) bytes
1 terabyte (TB) = 1012 bytes (1000 billion)
What is programming?
Programming is the process of creating a set of instructions — a
program — to tell a computer how to perform a task.
Programs take input data, perform some computation — numerical or
symbolic (text) — and produce output data.
Computers can perform only basic binary operations (such as add or
multiply two numbers)
How do we communicate complex instructions to computers? —
Use a programming language!
Levels of programming languages
Low-level languages High-level languages
Closer to machine, difficult for Closer to humans, easier for
humans humans to work with
Less portable, provide less More portable, more
abstraction over hardware abstraction over hardware
Examples: Assembly Language Examples: Java, Python
How do computers understand high-level languages?
High-level languages are translated into Machine code (for CPU).
Programming languages come in two main flavors — compiled
languages or interpreted languages:
Compiled (e.g. C/C++, Java) Interpreted (e.g. Python)
High-level program (source code) ➞ High-level program (source code) ➞
compiler ➞ binary executable Executed directly by interpreter
Once compiled, the binary program can The interpreter is required on the machine
be executed without compiler. where the program is executed.
Data in binary
Computers can understand only binary numbers
How can we encode data in the real world into binary numbers?
Data in real world
??? Integer & Real Numbers
Computers
Text (articles, books)
binary numbers Images
(0’s & 1’s) Audio/Video
Integers in binary
We already saw how to represent positive integers in binary e.g.
109 = 11011012
For signed integers (to differentiate negative and positive), an extra
leftmost bit is used for sign only, e.g.
−109 = 1 11011012
+109 = 0 11011012
(For more info: https://en.wikipedia.org/wiki/Signed_number_representations)
Real numbers in binary
64-bit Floating point format is used to represent numbers with
decimal point, e.g.
0 10000000000 1001001000011111101101010100010001000010110100011000 =
3.141592653589793
Floating point format has a finite precision, but digits of π run forever:
3.1415926535897932384626433832795028841...
But with only 64-bits we can only have precision up to a fixed digits
after decimal point: 3.141592653589793
(For more info: https://en.wikipedia.org/wiki/Double-precision_floating-point_format)
Text in binary
Letters and punctuations in human languages are encoded in binary using a
Character Encoding such as ASCII or UTF-8 (Unicode).
(source: https://simple.wikipedia.org/wiki/ASCII)
Images, audio & video in binary
Even for these complex data, the idea remains the same.
We use a format to store bytes in a file and programs know what to
do (play music, show image, etc) based on the format.
We already know some of these formats:
Images: jpeg, png
Audio: mp3, m4a, wma
Video: mp4, avi, wmv
Thonny Demo — Editor vs Shell
Python interpreter can work in an interactive or shell mode in which
lines of code are executed immediately as soon as they are entered
and output is visible immediately.
Shell mode is apparent whenever you see the prompt >>> .
In Script mode, a Python file ( .py ) is executed by interpreter as a
program (whole file, not line-by-line).
Thonny allows us to use both modes.
Comments
Comments are annotations we add to our program and are ignored by
the Python interpreter.
In Python, we start a comment using #.
1 # Author: Deven
2 # My first program
3
4 # This is a comment on its own line & it will be ignored
5 print("Hello, world!") # str
6 print(123) # int
7 print(1.614) # float
We use comments to:
Make the code easier to read and understand by explaining how it
works.
Indicate authorship and license.
Disable some code (prevent it from executing) but still keeping it in
the file.
In Thonny, we can use Edit menu -> Toggle comment to
comment/uncomment the selected lines.
Objects and Data Types
All data in a Python program is represented by objects. An object always
has a type (or class) associated with it.
1 >>> 5 1 # Using type() function to know the type of objects
2 5 2 >>> type(5)
3 <class 'int'>
3 >>> 3.1415
4 >>> type(3.1415)
4 3.1415
5 <class 'float'>
5 >>> "Hello"
6 >>> type("Hello")
6 'Hello' 7 <class 'str'>
An object’s type determines the operations that the object supports:
1 # objects of int type can be added using +
2 >>> 10 + 5
3 15
4
5 # But an object of type str cannot be added to an int using +
6 >>> "Hello" + 5
7 Traceback (most recent call last):
8 File "<pyshell>", line 1, in <module>
9 TypeError: can only concatenate str (not "int") to str
Summary
We saw the three basic data types in Python:
int : Integers such as ..., −1, 0, 1, 2, ...
float : Floating-point numbers such as −1.2, 3.14, etc.
str :Text data (a sequence of characters) such as “hello world”,
“Python”, etc.
The terms Object and Value are used interchangeably.
So are the terms Class and Type.