• No results found

Department of Computer Science

N/A
N/A
Protected

Academic year: 2022

Share "Department of Computer Science"

Copied!
29
0
0

Loading.... (view fulltext now)

Full text

(1)

CCB-253: P ROGRAMMING WITH P YTHON

1

Introduction

Dr. Mohammad Sajid

Department of Computer Science

Aligarh Muslim University, Aligarh-202002

(2)

T EXT R ESOURCES

Allen B. Downey, “Think Python: How to Think Like a Computer Scientist”, 2nd edition, Updated for Python 3, Shroff/O’Reilly Publishers

Dave Brueck, Stephen Tanner, “Python 2.1 Bible”, New York, NY

Guido van Rossum and Fred L. Drake Jr, “An Introduction to Python Revised and updated for Python 3.2”, Network Theory Ltd..

John V Guttag, “Introduction to Computation and Programming Using Python’’, Revised and expanded Edition, MIT Press.

Kenneth Alfred Lambert , “Fundamentals of Python: First Programs”, Cengage Learning.

2

(3)

O BJECTIVE

❑ To develop and debug programs in Python.

❑ To define Python functions and call them.

❑ To use Python data structures – lists, tuples, set, dictionaries.

❑ To do input/output with files in Python.

3

(4)

C OMPUTER

4

• An electronic device capable of

• Receiving data (Input)

• Performing Sequence of operations (Arithmetic calculations/logical decisions)

• Producing Information (Output)

Classical Computers works based on Stored-Program approach

(Von Neumann Computer)

(5)

P ROGRAM

5

A program is a set of instructions that tells a computer what to do in order to come up with a solution to a particular problem.

A program guides computer through orderly sets of actions specified by computer programmers

//Program in C-language

#include<stdio.h>

int main() {

int a, b, c;

printf("Enter two numbers to add\n");

scanf("%d%d", &a, &b);

c = a + b;

printf("Sum of the numbers = %d\n", c);

return 0;

}

(6)

S OFTWARE

Software

 Set of programs, libraries and non-executable data (Documentation)

 Used to operate computers and related devices

 Computer hardware and software require each other and neither can be realistically used on its own.

System Software

 A software designed to run computer’s hardware and to provide a platform to other software.

 Example:- Operating System, Device drivers, Basic input/output system (BIOS)

Application Software

 A software designed to perform activities for the end-user.

 Example:- Word processor, Web browser, etc.

6

(7)

P ROGRAMMING L ANGUAGE

• Programming Language

• Programs are written using a programming language.

• A programming language is a formal language designed to communicate instructions to a computer.

• Programming languages generally consist of instructions for a computer.

• Programming languages can be used to create programs that implement specific algorithms.

Why programming Language ?

• Would you prefer to give instructions to machines in binary commands ?

• Is there any other better method to give instructions to

computers ?

(8)

TYPES OF PROGRAMMING LANGUAGE

• Low-Level Languages

• Machine Language

• Assembly Language

• High-Level Languages

(9)

L OW - LEVEL L ANGUAGES

 Low-level languages are very close to how different hardware elements of a computer actually communicate with each other.

Low-level languages are machine oriented and require extensive knowledge of computer hardware and its configuration.

 There are two categories of low-level languages:

 Machine language

 Assembly language.

(10)

L OW - LEVEL L ANGUAGES

•Machine language is directly understood by the computer

•NO need to translate.

•All instructions use binary notation and are written as a string of 1s and 0s.

•Binary opcodes are difficult to remember and it is hard to tell the difference between data and instructions.

•Machine language is machine dependent.

(11)

A SSEMBLY LANGUAGE

Each instruction is presented in more human-readable form.

If e9 represents addition instruction; ‘add’ can be used in place of e9, called mnemonics.

But before executing, you have to convert assembly language into machine language.

Assembly language is machine dependent.

Example: 8085, 8086 assembly language.

(12)

H IGH - LEVEL P ROGRAMMING L ANGUAGE

•The problem of machine dependency should be removed.

•Need for more expressivity and portability.

•A high-level language is a programming language that uses English and mathematical symbols, like +, -, % and many others, in its instructions.

•Example:C, C++, Fortran, Java and Python.

C Language Program to add two numbers a and b

#include<stdio.h>

int main() {

int a, b, sum; // declare three integer variables printf("Enter two numbers..."); // ask user to enter two integers

scanf("%d%d",&a,&b); // reads two integers through keyboard sum=a + b; // calculate the sum

printf("The sum is %d", sum); // prints the sum return 0;

}

(13)

LANGUAGE TRANSLATORS

• Computer understands only machine language.

• A programming language translator is the piece of software that translate a computer program written in some specific programming language into another programming language.

• Language Translators

 Assembler

 Compiler

 Interpreter

(14)

LANGUAGE TRANSLATORS

Assembler

The assembler converts a program written in assembly language into machine code.

An assembler works by assembling and converting the source code of assembly language into object code.

An object file constitutes a stream of zeros and ones of machine code, which are directly executable by the processor.

Compiler

The compiler is a program that converts a program written into a high-level programming language into machine code.

The process of converting high-level programming into machine language is known as compilation.

It generates a file usually called object file which is read by machine.

Scans the entire program and translates it as a whole into machine code.

(15)

LANGUAGE TRANSLATORS

 INTERPRETER

An interpreter is a program that converts program written in high-level language into machine code understood by the computer.

It translates program one statement at a time and execute it.

No intermediate object code is generated.

(16)

PYTHON PROGRAMMING

Python is an interpreted, interactive, general purpose, object- oriented and high- level programming language.

• It is developed in 1989 by Guido van Rossum and first released in 1991.

• Python has a design philosophy that emphasizes

• Code readability

• Syntax that allows programmers to express concepts in fewer lines of code

• It provides constructs that enable clear programming on both

small and large scales.

(17)

G ETTING S TARTEDWITH P YTHON

17

• Python Web site at www.python.org

– Primary distribution center for Python source code, modules and documentation

• Go to http://www.python.org

• Get the latest distribution (3.8.1)

• Online tutorials

(18)

P YTHON IDLE

18

(19)

I NTEGRATED D EVELOPMENT E NVIRONMENT

19

•Anintegrated development environment (IDE) is a software suite that consolidates the basic tools developers need to write and test software.

Typically, an IDE contains

• A code editor

• A compiler/interpreter

• A debugger

•All three are accessible through a single graphical user interface (GUI).

•An IDE may be a standalone application, or it may be included as part

of one or more existing and compatible applications.

(20)

P YTHON IDE: IDLE

20

•It is an integrated development environment for

Python, which has been bundled with the default implementation of the language.

•It is packaged as an optional part of the Python packaging with many Linux distributions.

• It is completely written in Python and the Tkinter GUI toolkit.

(21)

R UNNING CODE IN THE INTERACTIVE SHELL

21

Python is an interpreted language, and simple Python expressions and statements can be run in an interactive programming environment

called the shell.

• At the prompt (>>>) type:

print (“Python: Hello World”) – Press [Enter]

• Very straightforward

– You could have guessed what this does without knowing Python!

(22)

C ALCULATOR

22

1.Start the Python interpreter and use it as a calculator i.e. Use interactive shell to perform the following operations

2 + 3,

5*3,

7-2,

6/2,

6/4,

6%4,

6%2,

2*4-3,

4-2*6-4

(23)

P ROGRAMMING IN S CRIPT M ODE

 Interactive mode gives you immediate feedback

 Not designed to create programs to save and run later

 Script Mode

 Write, edit, save, and run (later)

 Word processor for your code.

 Script Mode Procedure—

 Open a New File from the File menu of the IDLE shell window

 Write your program and save your file using the “.py”

extension

 Use Run Module (F5) from the Run menu of IDLE shell

window

23

(24)

P ROGRAM

24

1.Write a program in Python programming and save it firstProgram.py to perform the following operations 2 + 3, 5*3, 7-2, 6/2, 6/4, 6%4, 6%2

2.Write a program in Python programming and save it

secondProgram.py to display the following messages “Hello World, Python is High level, General-purpose Programming

language”

“ Guido Van Rossum invented the Python programming language in

1990s”

(25)

P YTHON IS CASE - SENSITIVE

25

• Python is “case-sensitive”

– print(“Python: Hello World”) – Print(“Python: Hello World”)

– PRINT(“Python: Hello World”)

(26)

• Comment lines provide documentation about your program – Anything after the “#” symbol is a comment

– non-executable (Ignored by the python shell)

• Example

#I am Python Programmer

#First Python Program

#January 21, 2019

• Multi-line Comment can also be used

– Multiple lines between two triple quotes are ignored by the python shell

– It is considered as string.

• Example

’’’I am Python Programmer First Python Program

January 21, 2019’’’

P ROGRAM D OCUMENTATION

26

(27)

Simple

▪ Python is a simple and minimalistic language.

▪ Reading a good Python program feels almost like reading English (but very strict English!).

▪ This pseudo-code nature of Python is one of its greatest strengths.

It allows you to concentrate on the solution of the problem rather than the syntax to write the code.

Easy to Learn

▪ Python is extremely easy to get started with.

▪ Python has an extraordinarily simple syntax as already mentioned.

Free and Open Source

▪ Python is an example of a FLOSS (Free/Libre and Open Source Software).

▪ In simple terms, you can freely

distribute copies of this software,

read the software's source code,

make changes to it,

use pieces of it in new free programs

know you can do these things.

High-level Language

▪ When you write programs in Python, you never need to bother about low-level details such as managing the memory used by your program.

GUI Programming

▪ Python supports GUI applications that can be created and ported to many system calls, libraries and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.

F EATURES OF P YTHON

27

(28)

Portable

▪ Due to its open-source nature, Python has been ported (i.e. changed to make it work on) to different platforms.

▪ All your Python programs will work on any of these platforms without requiring any changes at all.

▪ Linux, Windows, Macintosh, Solaris, OS/2, Amiga, AROS, AS/400, BeOS, OS/390, z/OS, Palm OS, QNX, VMS, Psion, Acorn RISC OS, VxWorks, PlayStation, Sharp Zaurus, Windows CE and PocketPC !

Interpreted

▪ This requires a little explanation.

Compiled Language

A program written in a compiled language is translated from the source language into machine language. When you run the program, the linker/ loader software just stores the binary code in the computer's memory and starts executing from the first instruction in the program.

Interpreted Language

▪ When you use an interpreted language like Python, there is no separate compilation and execution steps. You just run the program from the source code. Internally, Python converts the source code into an intermediate form called bytecodes and then translates this into the native language of computer and then runs it.

▪ All this makes using Python so much easier. You just run your programs - you never have to worry about linking and loading with libraries, etc.

Interactive

▪ You can interact directly with the python interpreter directly.

F EATURES OF P YTHON

28

(29)

Object Oriented

Python supports procedure-oriented programming as well as object-oriented programming.

▪ In procedure-oriented languages, the program is built around procedures or functions which are nothing but reusable pieces of programs.

▪ In object-oriented languages, the program is built around objects which combine data and functionality.

▪ Python has a very powerful but simple way of doing object-oriented programming, especially, when compared to languages like C++ or Java.

Extensible

▪ If you need a critical piece of code to run very fast, you can achieve this by writing that piece of code in C, and then combine that with your Python program.

Extensive Libraries

▪ The Python Standard Library is huge indeed.

▪ It can help you do various things involving regular expressions, documentation generation, unit testing, threading, databases, web browsers, CGI, ftp, email, XML, XML-RPC, HTML, WAV files, cryptography, GUI(graphical user interfaces) using Tk, and also other system-dependent stuff.

▪ All of this is always available with Python. This is called the "batteries included" philosophy of Python.

▪ Besides the standard library, there are various other high-quality libraries such as the Python Imaging Library which is an amazingly simple image manipulation library.

F EATURES OF P YTHON

Thank You!!!!!

29

References

Related documents

 The compiler is a program that converts a program written into a high-level programming language into machine code.  The process of converting high-level programming into

This Java program is supplying to JPCT (Java program code transformer) which gives the output program called transformed program J 0 which is feed as input to JCUTE (Java

The repeated portions of the source code will be placed in abstract procedures and a call to these abstract procedures is made instead of using the repeated

For automated test input data generation, we are using an advanced code transformer which is an improvement on Boolean code transformer and Program code transformer by using

Program Slicing is selection a group of statements from the initial program statements which are going to be effected if a change is made in the given input statement based on

Automatic summarization is the process of reducing a text Document with a computer program in order to create a summary that retains the most important points of

Department of Mechanical Engineering, N.I.T Rourkela Page 31 A computer program in C language was written based on Ant Colony Optimization algorithm to get the optimum

Dosimetric model of human lung and associated computer program %$' function of aerosols diameter is given in ICRP Publication 66 in the form of graphs and