0% found this document useful (0 votes)
5 views

Mod - 3-Python and Its Environment

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

Mod - 3-Python and Its Environment

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

PYTHON AND ITS

ENVIRONMENT
(MODULE 3)

2

3
OBJECTIVES

▰ Demonstrate and discuss the Python Environment


▰ Identify the python operators, variables, data types
▰ Create a basic python program

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

▰ Python was conceived in the late 1980s.


▻ Guido van Rossum, Benevolent Dictator For Life
▻ Rossum is Dutch, born in Netherlands, Christmas break
bored, big fan of Monty python’s Flying Circus
▻ Descendant of ABC, he wrote glob() func in UNIX
▻ M.D. @ U of Amsterdam, worked for CWI, NIST, CNRI, Google
▻ Also, helped develop the ABC programming language
▰ In 1991 python 0.9.0 was published and reached the masses through alt.sources
8
PYTHON TIMELINE

▰ In January of 1994 python 1.0 was released


▻ Functional programming tools like lambda, map, filter, and reduce comp.lang.python
formed, greatly increasing python’s userbase
▰ In 2000, Python 2.0 was released.
▻ Introduced list comprehensions similar to Haskells
▻ Introduced garbage collection
▰ In 2001, Python 2.2 was released.
▻ Included unification of types and classes into one hierarchy, making pythons object
model purely Object-oriented
▻ Generators were added(function-like iterator behavior) 9
PYTHON TIMELINE

▰ In January of 1994 python 1.0 was released


▻ Functional programming tools like lambda, map, filter, and reduce comp.lang.python
formed, greatly increasing python’s userbase
▰ In 2000, Python 2.0 was released.
▻ Introduced list comprehensions similar to Haskells
▻ Introduced garbage collection
▰ In 2001, Python 2.2 was released.
▻ Included unification of types and classes into one hierarchy, making pythons object
model purely Object-oriented
▻ Generators were added(function-like iterator behavior) 10
PYTHON INSTALLATION

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

▰ Python has many operators. Some examples are: +, -, *, /, %, >, <, ==


▰ Operators perform an action on one or more operands. Some operators
accept operands before and after themselves: operand1 + operand2, or 3 + 5
▰ Others are followed by one or more operands until the end of the line, such as:
print “Hi!”, 32, 48
▰ When operators are evaluated, they perform action on their operands, and
produce a new value.

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

▰ In Python, all data has an associated data “Type”.


▰ You can find the “Type” of any piece of data by using the type() function:
type( “Hi!”) produces <type 'str'>
type( True ) produces <type 'bool'>
type( 5 ) produces <type 'int'>
type(5.0) produces <type 'float'>
▰ Note that python supports two different types of numbers, Integers (int) and
Floating point numbers (float). Floating Point numbers have a fractional part
(digits after the decimal place), while Integers do not!
21
Effect of Data Types on Operator Results

▰ Math operators work differently on Floats and Ints:


int + int produces an int
int + float or float + int produces a float
▰ This is especially important for division, as integer division produces a
different result from floating point division:
10 / 3 produces 3
10.0 / 3.0 produces 3.3333333
▰ Other operators work differently on different data types: + (addition) will add
two numbers, but concatenate strings.
22
Simple Data types in Python

The simple data types in Python are:


▰ Numbers

▻ int – Integer: -5, 10, 77


▻ float – Floating Point numbers: 3.1457, 0.34
▰ bool – Booleans (True or False)‫‏‬
▰ Strings are a more complicated data type (called Sequences) that we will
discuss more later. They are made up of individual letters (strings of length 1)‫‏‬

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

▰ Variables are names that can point to data.


▰ They are useful for saving intermediate results and keeping data
organized.
▰ The assignment operator (=) assigns data to variables.
 Don't confuse the assignment operator (single equal sign, =) with the
Equality-Test operator (double equal sign, ==)
▰ Variable names can be made up of letters, numbers and
underscores, and must start with a letter.
26
PYTHON VARIABLES

▰ When a variable is evaluated, it produces the value of the data


that it points to.
For example:
myVariable = 5
myVariable produces 5
myVariable + 10 produces 15
▰ You MUST assign something to a variable (to create the variable
name) before you try to use (evaluate) it.
27
3
BASIC PYTHON
PROGRAMMING
Python and Its Environment
28
PYTHON ACCEPTS INPUT FROM THE USER

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

Using Notepad++ and


CMD

Python Program that accepts name and age of a person 30


Program 1 Using IDLE (shell python)

Python Program that accepts name and age of a person 31


Program 1 Locate in your drive…

Python Program that accepts name and age of a person 32


Program 1

Once you open


the file… run the
module.

Python Program that accepts name and age of a person 33


Program 1

Once you open


the file… run the
module.

Python Program that accepts name and age of a person 34


Program 2

Python Program that accepts 2 integers and display the sum 35


Program 3

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/

 Credits to Jay Summet of IPRE (PPT Review of Python)

37
THANKS!
Any questions?

38
CREDITS

Special thanks to all the people who made and


released these awesome resources for free:
▰ Presentation template by SlidesCarnival
▰ Photographs by Startup Stock Photos

39

You might also like