Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views64 pages

CF Chapter-005

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 64

Chapter No 5 : Introduction to Programming Environment

C E – 119
Computing Fundamentals (CF)

Compiled By:

Sir Syed University of Engineering & Technology


Engr. Syed Atir Iftikhar
Computer Engineering Department satir@ssuet.edu.pk
University Road, Karachi-75300, PAKISTAN 1
CE - 119 : Computing Fundamentals (CF)
 Course Objectives:
 This course covers the concepts and fundamentals
of computing and programming. Topics includes
history, components of computers, hardware,
software, operating systems, database, networks,
number systems and logic gates. Also it includes
programming topics such as basic building blocks,
loop, decision making statements.

2
CE - 119 : Computing Fundamentals (CF)
 Course Learning Outcomes ( CLO )
CLO Level
Outcome Statement
No. *
Explain the fundamental knowledge and concepts about
1 computing infrastructure including hardware, software, C2
database and networks.
Applying and Implementing number systems and logic
2 C3
gates.

Applying and Implementing problem solving skills and


3 C3
solve problems incorporating the concept of programming.
3
Books

 Text Books
1. Computing Essentials, Timothy O’Leary and Linda O’Leary
2. Introduction to Computers, Peter Norton, 6th Edition, McGraw-Hill
3. Introduction to Programming using Python, Daniel Liang
4. Turbo C Programming For The PC, Robert Lafore, Revised Edition

 Reference Books:
1. Discovering Computers, Misty Vermaat and Susan Sebok,
Cengage Learning
2. Using Information Technology: A Practical Introduction to Computers
& Communications, Williams Sawyer, 9th Edition, McGraw-Hill
3. Introduction to Python, Paul Deitel, Harvey Deitel
4. Let Us C, Yashavant Kanetkar 4
Marks Distribution

 Total Marks ( Theory ) ___________ 100


 Mid Term ___________ 25
 Assignments + Quizzes + Presentation ___________ 25
 Semester Final Examination Paper ___________ 50

 Total Marks ( Laboratory ) ___________ 50


 Lab File ___________ 15
 Subject Project ___________ 15
 Lab Exam/Quiz ( Theory Teacher ) ___________ 20

 https://sites.google.com/view/muzammil2050

5
Course Instructors

 Muzammil Ahmad Khan muzammil.ssuet@yahoo.com


Assistant Professor, CED
Room No: BS-04

 Atir Iftikhar satir@ssuet.edu.pk


Lecturer, CED
Room No: BT-05

6
CE – 119: Computing Fundamentals Chapter

5
Introduction to
Programming Environment

Compiled By:
Engr. Syed Atir Iftikhar satir@ssuet.edu.pk
7
Objectives
 To write and run a simple Python program
 To explain the basic syntax of a Python program
 To describe the history of Python
 To explain the importance of, and provide examples of,
proper programming style and documentation
 To explain the differences between syntax errors,
runtime errors, and logic errors
 To create a basic graphics program using Turtle
8
Computer Programming

 Computers do “what we tell them to do”, NOT


“what we want them to do”.

 Computer Programming involves writing


instructions and giving them to the computer to
complete a task.

9
Programs

 Computer programs, known as software, are instructions


to the computer.
 You tell a computer what to do through programs.
Without programs, a computer is an empty machine.
 Computers do not understand human languages, so you
need to use computer languages to communicate with
them.
 Programs are written using programming languages.
10
Programming Languages
Machine Language Assembly Language High-Level Language

 Machine language is a set of primitive instructions built


into every computer.
 The instructions are in the form of binary code, so you
have to enter binary codes for various instructions.
Program with native machine language is a tedious
process.
 Moreover the programs are highly difficult to read and
modify. For example, to add two numbers, you might
write an instruction in binary like this:

1101101010011010
11
Programming Languages
Machine Language Assembly Language High-Level Language

 Assembly languages were developed to make


programming easy. Since the computer cannot
understand assembly language, however, a program
called assembler is used to convert assembly language
programs into machine code. For example, to add two
numbers, you might write an instruction in assembly
code like this:
ADDF3 R1, R2, R3 Assembly Source File
Machine Code File


Assembler …
ADDF3 R1, R2, R3
1101101010011010

12
Programming Languages
Machine Language Assembly Language High-Level Language

 The high-level languages are English-like and easy to


learn and program.

 For example, the following is a high-level language


statement that computes the area of a circle with a value
of radius 5:

area = 5 * 5 * 3.1415;
13
Popular High-Level Languages
 COBOL (COmmon Business Oriented Language)
 FORTRAN (FORmula TRANslation)
 BASIC (Beginner All-purpose Symbolic Instructional Code)
 Pascal (named for Blaise Pascal)
 Ada (named for Ada Lovelace)
 C (whose developer designed B first)
 Visual Basic (Basic-like visual language developed by Microsoft)
 Delphi (Pascal-like visual language developed by Borland)
 C++ (an object-oriented language, based on C)
 C# (a Python-like language developed by Microsoft)
 Python (We use it in the book)
14
Compiling Source Code
 A program written in a high-level language is called a
source program. Since a computer cannot understand a
source program.
 Program called a compiler is used to translate the
source program into a machine language program
called an object program. The object program is often
then linked with other supporting library code before
the object can be executed on the machine.

Source File Compiler Machine-language


Linker Executable File
File

Library Code

15
Operating Systems
 The operating system (OS) is a
program that manages and controls User
a computer’s activities. You are
probably using Windows 10 or Application Programs

previous versions of Windows.


Operating System
 Windows is currently the most
popular PC operating system. Hardware

Application programs such as an


Internet browser and a word
processor cannot run without an
operating system.
16
Algorithm and Pseudo code

 An Algorithm is sequence of steps, by which a


computer can produce the required outputs from the
available inputs.

 “An effective procedure for solving a class of


problems in a finite number of steps.”

 Pseudo code is an artificial and informal language


that helps programmers develop algorithms.
Pseudo code is very similar to everyday English.
17
Flow Chart

 A Flowchart is a visual representation of an algorithm

 A Flowchart uses easy-to-understand symbols to


represent actions on data and the flow of data

 Flowcharts aid in breaking down a problem into


simple steps

18
Flow Chart
Name Symbol Use in Flowchart

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


19
Flow Chart

Terminator Process Input / Output

Decision
Connector Data Flow

20
Algorithm and Pseudo code (Example 1)

 Write an algorithm to determine a student’s final


grade and indicate whether it is passing or failing.
The final grade is calculated as the average of four
marks.

21
Algorithm and Pseudo code (Example 1)

 Pseudo code:
1. Input a set of 4 marks
2. Calculate their average by summing and dividing by 4
3. if average is below 50
4. Print “FAIL”
5. else
6. Print “PASS”

22
Algorithm and Pseudo code (Example 1)

 Algorithm
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif 23
Algorithm and Pseudo code (Example 1)
START

Input M1,M2,M3, M4

GRADE  ( M1 + M2 + M3 + M4 ) / 4

N Y
IS GRADE <
50

PRINT PRINT
“ PASS ” “ FAIL ”

STOP 24
Algorithm and Pseudo code (Example2)

 Write an algorithm and draw a flowchart that will


read the two sides of a rectangle and calculate its
area.
 Pseudo code
1. Input the width (W) and Length (L) of a rectangle
2. Calculate the area (A) by multiplying L with W
3. Print A
25
Algorithm and Pseudo code (Example2)

 Algorithm START

Step 1: Input W , L Input


W, L
Step 2: AL x W
ALxW
Step 3: Print A
Print
A

STOP
26
3 Computer Programming Construct

 Any problem, regardless of how complex, can be


broken down into three basic CONSTRUCTS
1. SEQUENCE
2. SELECTION
3. ITERATION

27
3 Computer Programming Construct

F T F T

Sequence Selection Iteration

28
What is Python?
General Purpose Interpreted Object-Oriented

 Python is a general purpose programming language.


That means you can use Python to write code for any
programming tasks.

 Python are now used in Google search engine,


in mission critical projects in NASA, in processing
financial transactions at New York Stock Exchange.

29
What is Python?
General Purpose Interpreted Object-Oriented

 Python is interpreted, which means that python code is


translated and executed by an interpreter one statement
at a time.

 In a compiled language, the entire source code is


compiled and then executed altogether.

30
What is Python?
General Purpose Interpreted Object-Oriented

 Python is an object-oriented programming language.


Data in Python are objects created from classes.

 A class is essentially a type that defines the objects of


the same kind with properties and methods for
manipulating objects.

 Object-oriented programming is a powerful tool for


developing reusable software.
31
Python’s History
 Created by Guido van Rossum in Netherlands in 1990

 Open source

32
Python 2 vs. Python 3
 Python 3 is a newer version, but it is not backward
compatible with Python 2.

 That means if you write a program using Python 2,


it may not work on Python 3.

33
Launch Python

34
Launch Python IDLE

35
Run Python Script

36
A Simple Python Program
Listing 1.1
# Display two messages

print("Welcome to Python")

print("Python is fun")

37
Creating and Editing Using Notepad
 To use Notepad, type
notepad Welcome.py
from the DOS prompt.

38
animation

Trace a Program Execution


Execute a statement

# Display two messages


print("Welcome to Python")
print("Python is fun")

39
animation

Trace a Program Execution


Execute a statement

# Display two messages


print("Welcome to Python")
print("Python is fun")

40
41
Program 1.1 WelcomeWithThreeMessages.py

# Display three messages

print("Welcome to Python")

print("Python is fun")

print("Problem Driven")

Output:
Welcome to Python
Python is fun
Problem Driven
42
Program 1.2 ComputeExpression.py

# Compute expression

print((10.5 + 2 * 3) / (45 – 3.5))

Output:
0.397590361446

43
Anatomy of a Python Program

 Statements

 Comments

 Indentation

44
Statement
A statement represents an action or a sequence of actions.
The statement print("Welcome to Python") in the
program in Listing 1.1 is a statement to display the
greeting "Welcome to Python“.
# Display two messages
print("Welcome to Python")
print("Python is fun")

45
Indentation
The indentation matters in Python. Note that the
statements are entered from the first column in the new
line. It would cause an error if the program is typed as
follows:
# Display two messages
print("Welcome to Python")
print("Python is fun")

46
Special Symbols
Character Name Description

() Opening and closing Used with functions.


parentheses
# Pound sign Precedes a comment line.

" " Opening and closing Enclosing a string (i.e., sequence of characters).
quotation marks
''' ''' Opening and closing Enclosing a paragraph comment.
quotation marks

47
Programming Style and Documentation

 Appropriate Comments

 Proper Indentation and Spacing Lines

48
Appropriate Comments

 Include a summary at the beginning of the program


to explain what the program does, its key features,
its supporting data structures, and any unique
techniques it uses.

 Include your name, class section, instructor, date,


and a brief description at the beginning of the
program.
49
Proper Indentation and Spacing

 Indentation
 Indent four spaces.
 A consistent spacing style makes programs clear
and easy to read, debug, and maintain.
 Spacing
 Use blank line to separate segments of the code.

50
Programming Errors

 Syntax Errors
 Error in code construction
 Runtime Errors
 Causes the program to abort
 Logic Errors
 Produces incorrect result

51
Escape Sequence
 Escape Sequences are used
Code Description
to signal an alternative
\’ Single Quote
interpretation of a series of
\” Double Quote
characters.
\\ Back Slash
 It interrupts the normal flow
\b Backspace
of output value.
 An escape character is a \n New Line

backslash \ followed by the \t Tab

character you want to insert. \r Carriage Return


Program 1.3 ComputeExpression.py

print("Greetings \nSir")

print("Welcome to the world of \"Programming\"")

print("Have a \rnice day...")

Output:
Greetings
Sir
Welcome to the world of "Programming"
nice day...
53
Getting Started with GUI Programming
Why GUI? Turtle Tkniter

 GUI is a great pedagogical tool to motivate students


and stimulate student interests in programming.

54
Getting Started with GUI Programming
Why GUI? Turtle Tkniter

 A simple way to start graphics programming is to use


Python built-in Turtle package.

55
Getting Started with GUI Programming
Why GUI? Turtle Tkniter

 Tkinter is used for developing comprehensive


GUI applications.

56
Getting Started with GUI Programming
Why GUI? Turtle

 Turtle is a pre-installed Python library that enables users to


create pictures and shapes by providing them with a virtual
canvas.
 The onscreen pen that you use for drawing is called the turtle
and this is what gives the library its name. ...
 Most developers use turtle to draw shapes, create designs, and
make images.
 In short, the Python turtle library helps new programmers get
a feel for what programming with Python is like in a fun and
interactive way.
57
Moving the Turtle
 There are four directions that a turtle can move in:
1. Forward
2. Backward
3. Left
4. Right
 The turtle moves .forward() or .backward() in the direction
that it’s facing. You can change this direction by turning it
.left() or .right() by a certain degree. You can try each of
these commands like so:
 >>> t.right(90)
 >>> t.forward(100)
58
Getting Started with GUI Programming
Why GUI? Turtle

 “Turtle” is a Python feature like a drawing board, which lets


us command a turtle to draw all over it!

 We can use functions like turtle.forward(…) and


turtle.right(…) which can move the turtle around.

 Commonly used turtle methods are :

59
Method Parameter Description
Turtle() None Creates and returns a new turtle object
forward() amount Moves the turtle forward by the specified amount
backward() amount Moves the turtle backward by the specified amount
right() angle Turns the turtle clockwise
left() angle Turns the turtle counterclockwise
penup() None Picks up the turtle’s Pen
pendown() None Puts down the turtle’s Pen
up() None Picks up the turtle’s Pen
down() None Puts down the turtle’s Pen
color() Color name Changes the color of the turtle’s pen
fillcolor() Color name Changes the color of the turtle will use to fill a polygon
heading() None Returns the current heading
position() None Returns the current position
goto() x, y Move the turtle to position x,y
begin_fill() None Remember the starting point for a filled polygon
end_fill() None Close the polygon and fill with the current fill color
dot() None Leave the dot at the current position
stamp() None Leaves an impression of a turtle shape at the current location
shape() shapename Should be ‘arrow’, ‘classic’, ‘turtle’ or ‘circle’ 60
Getting Started with GUI Programming
Why GUI? Turtle

Plotting using Turtle


To make use of the turtle methods and functionalities,
we need to import turtle.”turtle” comes packed with the
standard Python package and need not be installed externally.
The roadmap for executing a turtle program follows 4 steps:
1. Import the turtle module
2. Create a turtle to control.
3. Draw around using the turtle methods.
4. Run turtle.done().
61
62
63
64

You might also like