[go: up one dir, main page]

0% found this document useful (0 votes)
4 views37 pages

Lesson 1

This document serves as an introduction to computer programming, specifically focusing on Python. It covers the basics of programming, the differences between low-level and high-level languages, and the role of translators like interpreters and compilers. Additionally, it discusses Python's features, variable assignment, user input, and datatype conversion.
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)
4 views37 pages

Lesson 1

This document serves as an introduction to computer programming, specifically focusing on Python. It covers the basics of programming, the differences between low-level and high-level languages, and the role of translators like interpreters and compilers. Additionally, it discusses Python's features, variable assignment, user input, and datatype conversion.
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
You are on page 1/ 37

INTRODUCTION TO

COMPUTER
PROGRAMMING

Basic Elements of Python Programs


LECTURE 1
Getting Started with Python
Intended Learning Outcome
1. Able to use the print function to print an output

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.

WHAT IS COMPUTER PROGRAMMING?


▶ A computer system is made of electronic devices which operates from 0v to
5v. In computer design these voltages are divided into two states, which is the
High and Low. High corresponds to ranges close to 5v while the Low
corresponds to the voltages near 0v or ground. These states are converted into
a number system called binary. Thus the computer/machine language are a
series of binary digits (Bits) of 1 and 0.
▶ Since, it would be hard for human to give instruction to computer system using
the machine language. The computer programming was developed which is
base in human natural language.

THE LANGUAGE OF COMPUTER


▶ common language that a computer and human will understand
▶ There are a lot of programming language that have been develop but all of
these are divided into two types; Low-level Language and High-Level
Language.

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.

LOW LEVEL AND HIGH LEVEL


LANGUAGES
▶ programming language is similar to a natural language, the machine needs a
translator for it to understand.
▶ translator’s task is to translate the source code into computer codes which is
basically a set of binary numbers.

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

SIMPLE ASSIGNMENT STATEMENTS


▶ A literal is used to indicate a specific value, which can be assigned to
a variable

>>> 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

SIMPLE ASSIGNMENT STATEMENTS


▶ A simple way to view the effect of an assignment is to assume that when a variable changes, its old value
is replaced

>>> x = 2
>>> print(x) Befor Afte
2 e r
>>> x = 2.3 x
2 x = 2.3 2.3
>>> print(x)
2.3

SIMPLE ASSIGNMENT STATEMENTS:


BOX VIEW
▶ Python assignment statements are actually slightly different from the “variable as a box” model
▶ In Python, values may end up anywhere in memory, and variables are used to refer to them

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?

▶ In Python, input is accomplished via an assignment statement combined with a built-in


function called input

▶ 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:

>>> name = input("Enter your name: ")


Enter your name: Mohammad Hammoud
>>> name
'Mohammad Hammoud'
>>>
▶ Notice that whatever the user types is then stored as a string
▶ What happens if the user inputs a number?

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
>>>

And now a float


(no single quotes)!

ASSIGNING INPUT
▶ Here is another sample interaction with the Python interpreter:
>>> number = eval(input("Enter an equation: "))
Enter an equation: 3 + 2
>>> number
5
>>>

The eval function will evaluate this formula and


return a value, which is then assigned to the variable “number”

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

>>> number = int(input("Enter a number: "))


Enter a number: 3
An integer
>>> number
(no single quotes)!
3
>>>

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

>>> number = float(input("Enter a number: "))


Enter a number: 3.7
A float
>>> number
(no single quotes)!
3.7
>>>

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

>>> x = 10 >>> y = "20" >>> z = 30.0


>>> float(x) >>> float(y) >>> int(z)
10.0 20.0 30
>>> str(x) >>> int(y) >>> str(z)
'10' 20 '30.0'
>>> >>> >>>

integer float string float float integer


integer string string integer float string
DATATYPE CONVERSION
▶ Python allows us also to assign multiple values to multiple variables all at the same time

▶ 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 are case-sensitive


>>> x = 10
>>> X = 5.7
>>> print(x)
10
>>> print(X)
5.7

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

False class finally is return


None continue for lambda try
True def from nonlocal while
and del global not with
as elif if or yield
assert else import pass
break except in raise

IDENTIFIERS

Python
▶ Programs are composed of statements that are built from identifiers and expressions

▶ Identifiers are names


▶ They begin with an underscore or letter which can be followed by a combination of letter, digit, and/or
underscore characters
▶ They are case sensitive

▶ Expressions are the fragments of a program that produce data


▶ They can be composed of literals, variables, and operators

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…

You might also like