3. HISTORY
• Python was developed by Guido van Rossum in the late
80s to earlier 90s at the National Research Institute.
• Python is derived from many other languages, including
ABC, Modula-3, C, C++, Algol-68, SmallTalk and Unix shell.
• Python is copyrighted. Like Perl, Python source code is
now available under the GNU General Public License.
• Python is now maintained by a core development team at
the institute, although Guido van Rossum still directing its
progress.
4.
5. FEATURES
• Broad Standard Library.
• Portable.
• Extendable.
• Databases.
• Interactive Mode.
• Scalable.
• Integration.
6.
7. HOW TO GET PYTHON
The most up-to-date and current source code, binaries,
documentation, news, etc. is available at the official
website of Python:
Python Official Website : http://www.python.org/
To Download : http://www.python.org/download/
8. RUNNING PYTHON
• There are 3 ways to start python:
1.
Interactive Interpreter.
2.
Script from the command line.
3.
Integrated development Environment.
10. • Unix – IDLE is the very first Unix
IDE for Python.
• Windows – Pythonwin is the first
Windows Interface for Python
and is an IDE with a GUI.
• Macintosh – The Macintosh
version of Python along with the
IDLE IDE is available.
11. PROGRAM COMPONENTS
• Programs can read in data.
• Programs can write results.
• In between reading and writing, programs can
compute, that is, do arithmetic (or logic).
• test, that is, decide what to do next.
• loop, that is, do the same actions a number of times.
• delegate, that is, ask other parts of the program to perform some
task.
•
• Also, programs can ignore comments.
12. DOING SIMPLE ARITHIMETIC
• Here are the arithmetic operators:
• + performs addition
• - performs subtraction
• * performs multiplication
• / performs division
• When dividing two integers, the result is an integer: 14 / 5 is 2
• % performs modulus (remainder of division): 14 % 5 is 4
• ** performs exponentiation
• The result of doing arithmetic is often assigned to a variable:
sum = 10 + 22 + 13 + 44 + 72
• Variables can be used in arithmetic:
average = sum / 5
13. DECISION AND TESTS
• Your program can decide what to do by making a test
• The result of a test is a boolean value, True or False
• Here are tests on numbers:
•
•
•
•
•
•
< means “is less than”
<= means “is less than or equal to”
== means “is equal to”
!= means “is not equal to”
>= means “is greater than or equal to”
< means “is greater than”
• These same tests work on strings
•
All capital letters are “less than” all lowercase letters.
14. COMPOUND TESTS
• Boolean values can be combined with these operators:
•
•
•
and – gives True if both sides are True
or – gives True if at least one side is True
not – given True, this returns False, and vice versa
• Examples
•
•
score > 0 and score <= 100
name == “Abhi" and not score > 100
15. THE if STATEMENT
• The if statement evaluates a test, and if it is True, performs the
following indented statements; but if the test is False, it does
nothing
• Examples:
•
•
if grade == "A+":
print "Congratulations!"
if score < 0 or score > 100:
print "That’s not possible!"
score = input("Enter a correct value: ")
16. if WITH else
• The if statement can have an optional else part, to be performed
if the test result is False
• Example:
•
if grade == "A+":
print "Congratulations!"
else:
print "You could do so much better."
print "Your mother will be disappointed."
17. if WITH elif
• The if statement can have any number of elif tests
• Only one group of statements is executed—those controlled by the
first test that passes
• Example:
•
if grade == "A":
print "Congratulations!"
elif grade == "B":
print "That's pretty good."
elif grade == "C":
print "Well, it's passing, anyway."
else:
print "You really blew it this time!"
18. INDENTATION
• Indentation is required and must be consistent
• Standard indentation is 4 spaces or one tab
• IDLE does this pretty much automatically for you
• Example:
•
if 2 + 2 != 4:
print "Oh, no!"
print "Arithmethic doesn't work!"
print "Time to buy a new computer."
19. Lists AND ranges
• A list is a sequence of values enclosed in brackets
•
Example: courses = ['CIT 591', 'CIT 592', 'CIT 593']
• You can refer to an individual value by putting a bracketed number (starting from 0)
after the list
•
Example: courses[2] is 'CIT 593'
• The len function tells you how many things are in a list
•
Example: len(courses) is 3
• range is a function that creates a list of integers, from the first number up to but not
including the second number
•
Example: range(0, 5) creates the list [0, 1, 2, 3, 4]
• If you give range a third number, it is used as the step size
•
Example: range(2, 10, 3) creates the list [2, 5, 8]
20. THE for LOOP
• A for loop performs the same statements for each value in a list
•
Example:
for n in range(1, 4):
print "This is the number", n
prints
This is the number 1
This is the number 2
This is the number 3
• The for loop uses a variable (in this case, n) to hold the current
value in the list
21. SUMMARY
• Arithmetic: + - * / %
< <= == != >= >
• Logic (boolean): True False and or not
• Strings: "Double quoted" or 'Single quoted'
• Lists: [1, 2, 3, 4]
len(lst)
• Input: input(question)
range(0, 100, 5)
raw_input(question)
• Decide: if test: elif test: else:
• For loop: for variable in list:
• While loop: while test:
• Calling a function: sum(numbers)
• Defining a function: def sum(numbers): return result