Lesson 1
Lesson 1
COMPUTER
PROGRAMMING
MODULE 1. INTRODUCTION TO
COMPUTER PROGRAMMING
▶ Programming or Computer programming is a way of giving an instruction to a
computer to perform a specific task. In programming world, it often refer to as
coding.
▶ A sequence of instruction that a computer is executing is called computer
program. A set of computer programs can create a software.
▶ Computer in this course will not just be desktop and laptop, but all devices
capable of running computer programs.
PROGRAMMING LANGUAGES
▶ Low-Level Languages - It has no abstraction and the programming rely closely to the computer
architecture. To create a program using a low level language, the programmer must understand the
architecture of the computer system such as the number of registers, the size of memory, register and
cache, how the device are connected to each other, how many instruction the machine is capable and
what are these instructions.
Low level program codes are divided into two parts. the opcode and the operand.
▶ High- Level Languages - It has a high-level of abstraction and focus mainly on the programming logic
rather than the underlying hardware architecture. It is more user friendly and generally independent
from the computer hardware.
TRANSLATORS
TRANSLATOR CAN BE ANY OF THE
FOLLOWING:
▶ Interpreters
▶ An interpreter is a computer program that executes instructions written in a programming or scripting
language directly, without previously needing them to be translated into a machine language
program. An interpreter usually employs one of the following program execution strategies:
1. Parse source code and explicitly execute.
2. Translate the source code to some effective intermediate representation and execute it
immediately;
3. Execute the stored pre-compiled code created by a compiler that is part of the interpreter
program. Some of the most common programming language who still use interpreter are the
following:
Perl
Python
Matlab
Ruby
▶ Compilers
▶ The compiler is a computer program that converts machine code written in one programming language (source
language) into another language (target language). The term "compiler" is generally used by applications that
convert source code from a high-level programming language to a lower-level language (e.g. assembly
language, object code, or computer code) to construct an executable application.
▶ The most common language we used usually uses compilers are:
▶ C
▶ C++
▶ Java
▶ Visual Basic
▶ There are programming language that uses a hybrid of Interpreters and Compilers
▶ Assembler
▶ Assembly language (or assembler language), also abbreviated asm, is any
low-level programming language in which the instructions in the language and the
computer code instructions of the machine correlates quite strongly.
• Development started in the 1980’s by Guido van Rossum.
• Only became popular in the last decade or so.
• Python 2.x currently dominates, but Python 3.x is the future of Python.
• Interpreted, very-high-level programming language.
• Supports a multitude of programming paradigms.
• OOP, functional, procedural, logic, structured, etc.
• General purpose.
• Very comprehensive standard library includes numeric modules, crypto services, OS
interfaces, networking modules, GUI support, development tools, etc.
ABOUT PYTHON
• Easy to learn.
• Supports quick development.
• Cross-platform.
• Open Source.
• Extensible.
• Embeddable.
• Large standard library and active community.
• Useful for a wide variety of applications.
NOTABLE FEATURES
▶ In the following example, the parameter values passed to the print function are all technically
called literals
▶ More precisely, “Hello” and “Programming is fun!” are called textual literals, while 3 and 2.3 are called
numeric literals
>>> print("Hello")
Hello
>>> print("Programming is fun!")
Programming is fun!
>>> print(3)
3
>>> print(2.3)
2.3
LITERALS
▶ A literal is used to indicate a specific value, which can be assigned to
a variable
>>> x = 2
>>> print(x)
▪ x is a variable and 2 is its value 2
>>> x = 2.3
>>> print(x)
2.3
>>> x = 2
▪ x is a variable and 2 is its value >>> print(x)
2
▪ x can be assigned different values; >>> x = 2.3
hence, it is called a variable >>> print(x)
2.3
>>> x = 2
>>> print(x) Befor Afte
2 e r
>>> x = 2.3 x
2 x = 2.3 2.3
>>> print(x)
2.3
Before Afte
>>> x = 2 r
>>> print(x) x = 2.3 2
2
>>> x = 2.3 x 2
>>> print(x)
x 2.3
2.3 What will
happen to
SIMPLE ASSIGNMENT STATEMENTS: value 2?
ACTUAL VIEW
▶ Interestingly, as a Python programmer you do not have to worry about computer memory getting
filled up with old values when new values are assigned to variables
Afte
▶ Python will automatically clear old r
values out of memory in a process 2 Memory location
known as garbage collection
x X will be automatically
reclaimed by the
garbage collector
2.3
GARBAGE COLLECTION
▶ So far, we have been using values specified by programmers and printed or assigned to
variables
▶ How can we let users (not programmers) input values?
▶ When Python encounters a call to input, it prints <prompt> (which is a string literal) then
pauses and waits for the user to type some text and press the <Enter> key
<variable> = input(<prompt>)
ASSIGNING INPUT
▶ Here is a sample interaction with the Python interpreter:
ASSIGNING INPUT
▶ Here is a sample interaction with the Python interpreter:
>>> number = input("Enter a number: ")
Enter a number: 3
>>> number
'3'
>>>
▶ How can we force an input number to be stored as a number and not as a string?
▶ We can use the built-in eval function, which can be “wrapped around” the
input function
Still a
string!
ASSIGNING INPUT
▶ Here is a sample interaction with the Python interpreter:
>>> number = eval(input("Enter a number: "))
Enter a number: 3
>>> number
3
>>>
Now an int
(no single quotes)!
ASSIGNING INPUT
▶ Here is a sample interaction with the Python interpreter:
>>> number = eval(input("Enter a number: "))
Enter a number: 3.7
>>> number
3.7
>>>
ASSIGNING INPUT
▶ Here is another sample interaction with the Python interpreter:
>>> number = eval(input("Enter an equation: "))
Enter an equation: 3 + 2
>>> number
5
>>>
ASSIGNING INPUT
▶ Besides, we can convert the string output of the input function into an integer or a float using the
built-in int and float functions
DATATYPE CONVERSION
▶ Besides, we can convert the string output of the input function into an integer or a float using the
built-in int and float functions
DATATYPE CONVERSION
▶ As a matter of fact, we can do various kinds of conversions between strings, integers and floats using
the built-in int, float, and str functions
▶ This form of assignment might seem strange at first, but it can prove remarkably useful (e.g., for
swapping values)
>>> x, y = 2, 3
>>> x
2
>>> y
3
>>>
SIMULTANEOUS ASSIGNMENT
IDENTIFIERS
▶ Python has some rules about how identifiers can be formed
▶ Every identifier must begin with a letter or underscore, which may be followed by any sequence of
letters, digits, or underscores
▶ Python has some rules about how identifiers can be formed
IDENTIFIERS
▶ Python has some rules about how identifiers can be formed
▶ Some identifiers are part of Python itself (they are called reserved words or keywords) and cannot be
used by programmers as ordinary identifiers
IDENTIFIERS
Python
▶ Programs are composed of statements that are built from identifiers and expressions
SUMMARY
▶ A literal is a representation of a specific value (e.g., 3 is a literal representing the number three)
▶ A variable is an identifier that stores a value, which can change (hence, the name variable)
SUMMARY
▶ In Python, assignment of a value to a variable is done using the equal sign (i.e., =)
▶ Using assignments, programs can get inputs from users and manipulate them internally
▶ Datatype conversion involves converting implicitly and explicitly between various datatypes, including
integer, float, and string
SUMMARY
▶ OPERATORS IN PYTHON <3
NEXT LECTURE…