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

Lecture 1 Python

The document provides an introduction to Python, describing what Python is, its history and origins, how it differs from other programming languages, popular uses of Python, and how to install Python and write simple Python programs using an IDE like PyDev. It covers basic Python concepts like variables, data types, and code execution and provides examples of simple Python code.

Uploaded by

YOUSUF KHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Lecture 1 Python

The document provides an introduction to Python, describing what Python is, its history and origins, how it differs from other programming languages, popular uses of Python, and how to install Python and write simple Python programs using an IDE like PyDev. It covers basic Python concepts like variables, data types, and code execution and provides examples of simple Python code.

Uploaded by

YOUSUF KHAN
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

Introduction to

PYTHON
What We Give you?
• What is Python…?
• Differences between program and scripting language
• History of Python
• Scope of Python
• What can I do with python
• Who uses python today
• Why do people use Python?
• Installing Python IDE
• A Sample Code
• Python code execution
• Running Python
• Python Basic(Variable, Strings, Data types etc.)
2
What is Python…?
• Python is a general purpose high level, and object-oriented
programming language.
• Python is programming language as well as
scripting language.
• Dynamic typed
• Python is also called as Interpreted language

3
Differences between program and
scripting language

Program Scripting
• a program is executed (i.e. • a script is interpreted
the source is first compiled, • A "script" is code written in
and the result of that
compilation is expected) a scripting language. A
• A "program" in general, is a scripting language is nothing
sequence of instructions but a type of programming
written so that a computer language in which we can
can perform certain task. write code to control
another software
application.
4
History
• Invented in the Netherlands, early 90s by Guido
van Rossum
• Python was conceived in the late 1980s and
its implementation was started in December 1989
• Guido Van Rossum is fan of ‘Monty Python’s
Flying Circus’, this is a famous TV show in Netherlands
• Named after Monty Python
• Open sourced from the beginning

5
Python’s Benevolent Dictator For Life

“Python is an experiment in how


much freedom programmers need.
Too much freedom and nobody can
read another's code; too little and
expressiveness is endangered.”
- Guido van Rossum

6
Why was python created?
"My original motivation for creating Python was the
perceived need for a higher level language in the
Amoeba [Operating Systems] project.
I realized that the development of system
administration utilities in C was taking too long.
Moreover, doing these in the Bourne
wouldn't
things work for a variety of reasons. ... shell
So, there was a need for a language
that would bridge the gap between C and the shell”
- Guido Van
Rossum

7
What can I do with Python…?
• System programming
• Graphical User Interface Programming
• Internet Scripting
• Component Integration
• Database Programming
• Gaming, Images, XML , Robot and more

8
Scope of Python
Web Applications
• It provides libraries to handle internet protocols such as
HTML and XML, JSON, Email processing, request,
beautifulSoup, Feedparser etc.

• It also provides Frameworks such as Django, Pyramid, Flask


etc to design and develop web based applications.

• Some important developments are:


PythonWikiEngines
Pocoo
PythonBlogSoftware etc.

9
Scope of Python
Desktop GUI Applications
• Python provides Tk GUI library to develop user interface in
python based application

• Some other useful toolkits wxWidgets, Kivy, pyqt that are


useable on several platforms

• The Kivy is popular for writing multitouch applications.

10
Scope of Python
Scientific and Numeric
• Python is popular and widely used in scientific and numeric
computing.
• Some useful library and package are SciPy, Pandas, IPython
etc.
• SciPy is group of packages of engineering, science and
mathematics.

11
Scope of Python
Business Applications
• Python is used to build Bussiness applications like ERP and
e-commerce systems.

• Tryton is a high level application platform.

Audio or Video based Applications


• Python is awesome to perform multiple tasks and can be
used to develop multimedia applications.

• Some of real applications are: TimPlayer, cplay etc.

12
Scope of Python
3D CAD Applications
To create CAD application Fandango is a real application which
provides full features of CAD.

Enterprise Applications
• Python can be used to create applications which can be
used within an Enterprise or an Organization.
• Some real time applications are:
OpenErp, Tryton, Picalo etc.

13
Who uses python today…
• Python is being applied in real revenue-generating
products by real companies. For instance:
• Google makes extensive use of Python in its web
search system, and employs Python’s creator.
• Intel, Cisco, Hewlett-Packard, Seagate, Qualcomm, and IBM
use Python for hardware testing.
• ESRI uses Python as an end-user customization tool for its
popular GIS mapping products.
• The YouTube video sharing service is largely written
in Python

14
Why do people use Python…?
The following primary factors cited by Python users
seem to be these:
• Python is object-oriented
Structure supports such concepts as polymorphism,
operation overloading, and multiple inheritance.
.
• It's free (open source)
Downloading and installing Python is free and easy
Source code is easily accessible

15
• It's powerful
- Dynamic typing
- Built-in types and tools
- Library utilities
- Third party utilities (e.g. Numeric, NumPy, SciPy)
- Automatic memory management
• It's portable
- Python runs virtually every major platform used today
-As long as you have a compatible Python interpreter
installed, Python programs will run in exactly the same
manner, irrespective of platform.

16
Installing Python
• Python is pre-installed on most Unix systems, including Linux
and MAC OS X

• But for in Windows Operating Systems , user can


download from the
https://www.python.org/downloads/
- from the above link download latest version of
python IDE
Run the Installer

17
Go to:
https://www.python.org/downloads/

Click the “Download


Python 3.4.3
Button”
Installing Python

19
Python Interfaces
IDLE – a cross-platform Python development environment

PythonWin – a Windows only interface to Python


Python Shell – running 'python' from the Command Line opens
this interactive shell
PyDev -For Python development under Eclipse you can use the
PyDev Plugin which is an open source project.
Install PyDev via the Eclipse update manager via the following
update site: http://pydev.org/updates

We will use pyDev for python

20
Python Interfaces
IDLE – a cross-platform Python development environment

PythonWin – a Windows only interface to Python


Python Shell – running 'python' from the Command Line opens
this interactive shell
PyDev -For Python development under Eclipse you can use the
PyDev Plugin which is an open source project.
Install PyDev via the Eclipse update manager via the following
update site: http://pydev.org/updates

We will use pyDev for python

21
IDLE – Development Environment
IDLE helps you program
in Python by:
color-coding your program code
debugging
auto-indent
interactive shell
Example Python
Hello World
print “hello world”
Prints hello world to
standard out
Open IDLE and try it out
yourself
Follow along using IDLE
Configuration of Eclipse
You also have to maintain in Eclipse the location of your Python
installation. Open in the Window  Preference  Pydev 
Interpreter Python menu.

24
Your first Python program in Eclipse

Select File  New  Project. Select


Pydev → Pydev Project.
Your first Python program in Eclipse

Select the "src" folder of your project,


right-click it and select New →PyDev Modul.
Create a module "FirstModule" 
Your first Python program in Eclipse

Create the following source code.


Python Code Execution
• Python’s traditional runtime execution model: source code
you type is translated to byte code, which is then run by the
Python Virtual Machine. Your code is automatically
compiled, but then it is interpreted.

Source code extension is .py


Byte code extension is .pyc (compiled python code)
28
Python Syntax Rules
• Python is case sensitive. Hence a variable with name,First_name and
first_Name variables not same.
• For path specification, python uses forward slashes
C:/folderA/folder

• In python, there is no command terminator, which means no semicolon;

• In one line only a single executable statement should be written and the
line change act as command terminator in python.
• To write two separate executable statements in a single line, you should
use a semicolon ; to separate the commands. For example,

29
Python Syntax Rules
• In python, you can use single quotes ‘’ double quotes “ ” and even
triple quotes for string.

• In python, you can write comments in your program using a # , at the start.
A comment is ignored while the python script is executed.

30
Python Syntax Rules

• Line Continuation: To write a code in multiline


without confusing the python interpreter, is by using a
backslash / denote line continuation. For example

• Expressions enclosed in () []{} brackets don't need a


backward slash for line continuation. For example,
Python Syntax Rules

Code Indentation:This is the most important rule of python


programming. In programming language like Java, C or C++,
generally curly brackets {}are used to define a code block, but
python doesn't use brackets,
• It is recommended to use tab for indentation, although you
can use spaces for indentation as well, just keep in mind
that the amount of indentation for a single code block
should be same.
Python Syntax Rules
Also, the following code will give error, as the statements are
differently indented:

again, the correct way to do so is to keep all the statements of a


particular code block at same indentation
MATH(OPERATOR) IN PYTHON
Math
Try typing this into Code:
>>> print 3 + 12
15
>>> print 12 – 3
9
>>> print 9 + 5 – 15 + 12
11

Operators:
add: +
subtract: -

Note: don’t
type the
arrows >>> ! 35
Math
Rule: If you want Python to answer in floats, you have to
talk to it in floats.

More operators:
divide: /
multiply: *

>>> print 3 * 12 36
>>> print 12 / 3 4
>>> print 11 / 3 3
>>> print 12.0 / 3.0 4.0
>>> print 11.0 / 3.0 3.66
36
Math

Practice:

>>> print 2 < 3 True


>>> print 2 <= 2 False
>>> print 3 > 2 True
>>> print 2 != 3 True
>>> print False < True True

37
STRINGS IN PYTHON

38
Strings
Examples:
>>> “It’s a beautiful
day!”
>>> “Goodbye, cruel
Try typing one without quotes: world.”
What’s the result?
>>> Aggies

>>> “Aggies”
>>> “Rice fight,
never
die!”
>>> “3 + 2”
39
Strings

String operators:
concatenation: +
multiplication: *

Try concatenating: >>> print “Hello” +


Try multiplying: “ “ + “world!”
>>> print “HAHA” *
250

40
VARIABLES IN PYTHON

41
Variable
Create a Variable:
>>>headmaster=“Dumbledore”
>>>print headmaster
‘Dumbledore’

Assigning a New
Value:

>>>headmaster=“Har
dcastle”
>>>print headmaster
‘Hardcastle’

42
DATA TYPES IN PYTHON
Data Type:
Python has many native data types. Here are the important ones:

Booleans are either True or False.

Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions
(1/2 and 2/3), or even complex numbers.

Strings are sequences of Unicode characters, e.g. an HTML


document.

Bytes and byte arrays, e.g. a JPEG image file.

Lists are ordered sequences of values.

Tuples are ordered, immutable sequences of values.

Sets are unordered bags of values.


44
Example:

String “Whoop!”
Integer 42
Float 3.14159
List [“John”, “Paul”,
“George”, “Ringo”]
Python can tell us about types using the type()
function:

>>> print type(“Whoop!”)


<type ‘str’>

45
LIST: DATA TYPE

46
List:
The list is a most versatile Data type available in Python
which can be written as a list of comma-separated values
(items) between square brackets. Important thing about a
list is that items in a list need not be of the same type.

Example:
list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];

47
SN Function with Description

1 cmp(list1, list2) Compares elements of both lists.

2 len(list) Gives the total length of the list.

3 max(list) Returns item from the list with max value.

4 min(list) Returns item from the list with min value.

5 list(seq) Converts a tuple into list.


32
List: a sequence of objects
>>> Beatles = [“John”, “Paul”, “George”,
“Ringo”]
>>> grades = [82, 93, 67, 99, 100]

Guess what this will output:

>>> type(Beatles)

>>> type(grades)
49
Lists
Index: Where an item is in the list

>>> Beatles = [“John”, “Paul”, “George”,


“Ringo”]
>>> Beatles[0]
‘John‘

[“John”, “Paul”, “George”, “Ringo”]


0 2 3
1

Python always starts at zero!

50
TUPLE: DATA TYPE
Tuples
:A tuple is a sequence of immutable Python objects. Tuples are
sequences, just like lists. The differences between tuples and
lists are, the tuples cannot be changed unlike lists and tuples
use parentheses, whereas lists use square brackets.

Example:
tup2 = (1, 2, 3, 4, 5 );
tup3 = ("a", "b", "c", "d“);

Accessing Values:
print "tup2[1:5]: “
Output:
tup2[1:5]: [2, 3, 4,
5] 52
Built-in Tuple Functions
Python includes the following tuple functions −

SN Function with Description

1 cmp(tuple1, tuple2) Compares elements of both tuples.

2 len(tuple) Gives the total length of the tuple.

3 max(tuple) Returns item from the tuple with max value.

4 min(tuple) Returns item from the tuple with min value.

5 tuple(seq) Converts a list into tuple.

53
LOOPS & CONDITIONAL
STATEMENTS

54
Loop Type Description

while loop Repeats a statement or group of statements


while a given condition is TRUE. It tests the
condition before executing the loop body.

for loop Executes a sequence of statements multiple


times and abbreviates the code that
manages the loop variable.

nested loops You can use one or more loop inside any
another while, for or do..while loop.

39
Statement Description

if statements An if statement consists of a boolean expression


followed by one or more statements.

if...else statements An if statement can be followed by an


optional else statement, which executes when
the boolean expression is FALSE.

nested if statements You can use one if or else if statement inside


another if or else if statement(s).

56
I believe the trial has shown conclusively that it is both possible and
desirable to use Python as the principal teaching language:

o It is Free (as in both cost and source code).


o It is trivial to install on a Windows PC allowing students to take
their interest further. For many the hurdle of installing a Pascal or
C compiler on a Windows machine is either too expensive or too
complicated;
o It is a fl exible tool that allows both the teaching of traditi onal
procedural programming and modern OOP; It can be used to
teach a large number of transferable skills;
o It is a real-world programming language that can be and is used in
academia and the commercial world;
o It appears to be quicker to learn and, in combinati on with its many
libraries, this off ers the possibility of more rapid student
development allowing the course to be m ade more challenging
and varied;
o and most importantly, its clean syntax off ers increased
understanding and enjoyment for students;
57
42

You might also like