0% found this document useful (0 votes)
40 views

Computer Programming: Basic Elements of Python Programs

Python is an interpreted, high-level programming language that can be used for many types of programming. It has a simple syntax and comprehensive standard library. Programs written in high-level languages like Python are compiled by a translator like an interpreter or compiler into a low-level language that the computer can understand as binary code. Interpreters execute instructions directly without compilation while compilers translate source code into an executable program. Python code examples demonstrate textual and numeric literals passed to the print function.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Computer Programming: Basic Elements of Python Programs

Python is an interpreted, high-level programming language that can be used for many types of programming. It has a simple syntax and comprehensive standard library. Programs written in high-level languages like Python are compiled by a translator like an interpreter or compiler into a low-level language that the computer can understand as binary code. Interpreters execute instructions directly without compilation while compilers translate source code into an executable program. Python code examples demonstrate textual and numeric literals passed to the print function.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

INTRODUCTION TO

COMPUTER Basic Elements of Python

PROGRAMMING Programs
GETTING STARTED
WITH PYTHON
WOODGROVE 2
BANK
WHAT IS 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.

WOODGROVE 3
BANK
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.

WOODGROVE 4
BANK
THE LANGUAGE OF COMPUTERS

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.

WOODGROVE 5
BANK
COMPARISON
Low level languages and High Level Languages

LOW LEVEL LANGUAGES HIGH LEVEL LANGUAGES

• It has no abstraction and the programming • It has a high-level of abstraction and focus
rely closely to the computer architecture. To mainly on the programming logic rather than
create a program using a low level language, the underlying hardware architecture. It is
the programmer must understand the more user friendly and generally independent
architecture of the computer system such as from the computer hardware.
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.

WOODGROVE 6
BANK
TRANSLA
TORS
WOODGROVE 7
BANK
TRANSLATORS
• 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 IN DIFFERENT FORMS

INTERPRETERS
COMPILERS
ASSEMBLER
WOODGROVE 8
BANK
TRANSLATORS 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
WOODGROVE 9
BANK
TRANSLATORS 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

WOODGROVE 10
BANK
TRANSLATORS 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.

WOODGROVE 11
BANK
FACTS ABOUT PYTHON
• 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.
WOODGROVE 12
BANK
INTERESTING FACTS ABOUT PYTHON
• Python was a hobby project
In December 1989, Python‟s creator Guido
Van Rossum was looking for a hobby project
to keep him occupied in the week around
Christmas. He had been thinking of writing a
new scripting language that‟d be a
descendant of ABC and also appeal to
Unix/C hackers. He chose to call it Python.

• Why it was called Python


The language‟s name isn‟t about snakes, but
about the popular British comedy troupe
Monty Python (from the 1970s). Guido
himself is a big fan of Monty Python‟s
Flying Circus. Being in a rather irreverent
mood, he named the project „Python‟.

• Big Companies Using Python


NASA, Google, Nokia, IBM, Yahoo! Maps,
Walt Disney Feature Animation, Facebook,
Netflix, Spotify, Shutterstock, Uber,
Amazon, Mozilla, Dropbox, Pinterest and
Youtube

WOODGROVE 13
BANK
FACTS ABOUT PYTHON

WOODGROVE 14
BANK
PYTHON Python syntax and
codings
PROGRAMMING
WOODGROVE 15
BANK
WOODGROVE
BANK

In the following example, the parameter values passed to the print function are all
technically called literals

>>> print("Hello") TEXTUAL LITERALS


Hello

>>> print("Programming is fun!") TEXTUAL LITERALS

Programming is fun!

>>> print(3) NUMERIC LITERALS


3

>>> print(2.3) NUMERIC LITERALS


2.3

More precisely, “Hello” and “Programming is fun!” are called


textual literals, while 3 and 2.3 are called numeric literals
WOODGROVE
BANK

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

>>> print(x)
2.3
WOODGROVE
BANK

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
before
>>> print(x) X=2
2

>>> x = 2.3
after
Python assignment statements are actually slightly different
>>> print(x) X = 2.3 from the “variable as a box” model
2.3
In Python, values may end up anywhere in memory, and
variables are used to refer to them
WOODGROVE
BANK

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

Memory location will be automatically


reclaimed by the garbage collector 2
x
2.3

Python will automatically clear old values out of memory in a


process known as garbage collection
WOODGROVE
BANK

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 <variable> = input(<prompt>)
type some text and press the <Enter> key
WOODGROVE
BANK

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?
WOODGROVE
BANK

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

Here is a 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”
WOODGROVE
BANK

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

>>> number
3
WOODGROVE
BANK

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

>>> number
3.7
WOODGROVE
BANK

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

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

Suppose you have two variables x and y, and you want to swap their values
(i.e., you want the value stored in x to be in y and vice versa)

>>> x = 2
>>> y = 3
>>> x = y
>>> y = x

>>> x
3
>>> y
2
WOODGROVE
BANK

Python has some rules about how identifiers can be formed

Python has some rules about how identifiers can be formed


>>> x = 10
Identifiers are case-sensitive
>>> X = 5.7

>>> print(x)
10

>>> print(X)
5.7

Every identifier must begin with a letter or underscore, which


may be followed by any sequence of letters, digits, or
underscores
WOODGROVE
BANK

Python has some rules about how identifiers can be formed

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

Some identifiers are part of Python itself (they are called


reserved words or keywords) and cannot be used by
programmers as ordinary identifiers
SUMMARY
WOODGROVE 31
BANK
WOODGROVE
BANK

• 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

• 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)
WOODGROVE
BANK

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

Python allows simultaneous assignments, which are useful for swapping values of
variables

Datatype conversion involves converting implicitly and explicitly between various


datatypes, including integer, float, and string
WOODGROVE
BANK

THANK YOU!
+63 966 926 6290
SIR TATTS
cp.talusan@gmail.com

You might also like