Mod - 3-Python and Its Environment
Mod - 3-Python and Its Environment
ENVIRONMENT
(MODULE 3)
“
2
“
3
OBJECTIVES
4
1
INTRODUCTION ABOUT
PYTHON
Python and Its Environment
5
PYTHON CHARACTERISTICS
▰ Simple
▻ Python is a simple and minimalistic language in nature
▻ Reading a good python program should be like reading English
▻ Its Pseudo-code nature allows one to concentrate on the problem rather than the language
▰ Easy to Learn
▰ Free & Open source
▻ Freely distributed and Open source
▻ Maintained by the Python community
▰ High Level Language –memory management
▰ Portable – *runs on anything c code will 6
PYTHON CHARACTERISTICS
▰ Interpreted
▻ You run the program straight from the source code.
▻ Python program Bytecode a platforms native language
▻ You can just copy over your code to another system and it will auto-magically work! *with python
platform
▰ Object-Oriented
▻ Simple and additionally supports procedural programming
▰ Extensible – easily import other code
▰ Embeddable –easily place your code in non-python programs
▰ Extensive libraries
▻ (i.e. reg. expressions, doc generation, CGI, ftp, web browsers, ZIP, WAV, cryptography,
7
(wxPython, Twisted, Python Imaging library)
PYTHON TIMELINE
11
PYTHON SHELL (IDLE PYTHON)
When commands are read from a tty, the interpreter is said to be in interactive mode. In this mode it
prompts for the next command with the primary prompt, usually three greater-than signs (>>>); for
continuation lines it prompts with the secondary prompt, by default three dots (...). The interpreter prints a
welcome message stating its version number and a copyright notice before printing the first prompt:
12
PYTHON SHELL (IDLE PYTHON)
Printing Text:
13
PYTHON USING CMD AND NOTEPAD++
Locate Python
in your cmd:
14
PYTHON USING CMD AND NOTEPAD++
15
2
OPERATORS, DATA
TYPES AND VARIABLES
Python and Its Environment
16
PYTHON OPERATORS
17
Example Expression Evaluations
An expression is any set of values and operators that will produce a new value when
evaluated. Here are some examples, along with the new value they produce when
evaluated:
5 + 10 produces 15
“Hi” + “ “ + “Kath!” produces “Hi Kath!”
10 / (2+3) produces 2
10 > 5 produces True
10 < 5 produces False
10 / 3.5 produces 2.8571428571
10 / 3 produces 3
10 % 3 produces 1
18
List of Operators
▰ Some operators should be familiar from the world of mathematics such as Addition
(+), Subtraction (-), Multiplication (*), and Division (/).
▰ Python also has comparison operators, such as Less-Than (<), Greater-Than (>), Less-
Than-or-Equal(<=), Greater-Than-or-Equal (>=), and Equality-Test (==). These
operators produce a True or False value.
▰ A less common operator is the Modulo operator (%), which gives the remainder of an
integer division. 10 divided by 3 is 9 with a remainder of 1:
10 / 3 produces 3, while 10 % 3 produces 1
19
Operator Overloading
NOTE! Some operators will work in a different way depending upon what their
operands are. For example, when you add two numbers you get the expected result:
3 + 3 produces 6.
But if you “add” two or more strings, the + operator produces a concatenated version
of the strings:
“Hi” + “Kath” produces “HiKath”
Multiplying strings by a number repeats the string!
“Hi Kath” * 3 produces “Hi KathHi KathHi Kath”
The modulo operator also works differently with strings:
“test %f” % 34 produces “test 34.000”
20
PYTHON DATA TYPES
23
Type Conversion
▰ Data can sometimes be converted from one type to another. For example, the
string “3.0” is equivalent to the floating point number 3.0, which is equivalent to
the integer number 3
▰ Functions exist which will take data in one type and return data in another type.
▻ int() - Converts compatible data into an integer. This function will truncate
floating point numbers
▻ float() - Converts compatible data into a float.
▻ str() - Converts compatible data into a string.
24
Type Conversion
Examples:
int(3.3) produces 3
str(3.3) produces “3.3”
float(3) produces 3.0
float(“3.5”) produces 3.5
int(“7”) produces 7
int(“7.1”) throws an ERROR!
float(“Test”) throws an ERROR
25
PYTHON VARIABLES
input():
▰ In 3.x .raw_input() is renamed to input(). input() function reads a
line from sys.stdin and returns it with the trailing newline
stripped.
▰ print input() ('Input your name: ') prints out Input your name:
▰ To assign the user's name to a variable "x" you can use following
command : x = raw_input('Input your name: ')
29
Program 1
Python Program that accepts grades, then compute and display the average 36
REFERENCES
Online Resources:
http://it.metr.ou.edu/byteofpython/features-of-python.html
http://codesyntax.netfirms.com/lang-python.htm
http://www.python.org/
Sebesta, Robert W., Concepts of Programming Languages: 8th ed. 2007
http://www.python.org/~guido/
37
THANKS!
Any questions?
38
CREDITS
39