What is Python…?
• Python is a general purpose programming language that is often applied in scripting roles.
• So, Python is programming language as well as scripting language.
• Python is also called as Interpreted language
Differences between program and scripting language
a program is executed (i.e.
the source is first compiled,
and the result of that compilation is
expected)
• A "program" in general, is a
sequence of instructions
written so that a computer
can perform certain task.
a script is interpreted
• A "script" is code written in
a scripting language.
A scripting language is nothing
but a type of programming
language in which we can
write code to control
another software application.
What can I do with Python…?
• System programming
• Graphical User Interface Programming
• Internet Scripting
• Component Integration
• Database Programming
• Gaming, Images, XML , Robot and more
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
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
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.
• Python programs must be written with a particular
structure. The syntax must be correct, or the
interpreter will generate error messages and not
execute the program.
For example
print(“VKS-Learning Hub“ )
We will consider two ways in which we can run this
statement
• 1. enter the program directly into IDLE’s interactive
shell and
• 2. enter the program into IDLE’s editor, save it, and run
it.
• IDLE’s interactive shell. IDLE is a simple Python integrated
development environment available for Windows, Linux, and Mac
OS X. To start IDLE from the Microsoft Windows Start menu.
The IDLE interactive shell will open with >>> prompt.
You may type the above one line Python program directly
into IDLE and press enter to execute the program.
the result will be display using the IDLE interactive shell.
Since it does not provide a way to save the code you enter, the interactive shell is not the
best tool for writing larger programs. The IDLE interactive shell is useful for
experimenting with small snippets of Python code
IDLE’s editor. IDLE has a built in editor.
From the IDLE menu, select New Window,
Editor will open a file . Type the text print(“Faips Kuwait”) into the editor.
You can save your program using the Save option in the File menu as shown in
Figure. Save the code to a file named try1.py. The extension .py is the extension
used for Python source code.
We can run the program from within the IDLE editor by pressing the F5
function key or from the editor’s Run menu: Run→Run Module. The output
appears in the IDLE interactive shell window.
print(“VKS-Learning Hub")
This is a Python statement. A statement is a command that the interpreter executes. This statement
prints the message VKS-Learning Hub on the screen. A statement is the fundamental unit of
execution in a Python program. Statements may be grouped into larger chunks called blocks, and
blocks can make up more complex statements. Higher-order constructs such as functions and
methods are composed of blocks. The statement print(“VKS-Learning Hub") makes use of a built in
function named print
If you try to enter each line one at a time into the IDLE interactive shell, the program’s
output will be intermingled with the statements you type. In this case the best approach
is to type the program into an editor, save the code you type to a file, and then execute
the program. Most of the time we use an editor to enter and run our Python programs.
The interactive interpreter is most useful for experimenting with small snippets of Python
code.
It is important that no whitespace (spaces or tabs) come before the beginning of each
statement.
In Python the indentation of statements is significant and must be done properly. If
we try to put a single space before a statement in the interactive shell
The interpreter reports a similar error
when we attempt to run a saved Python
program if the code contains such
extraneous indentation.
Values and Variables
Python supports a number of numeric and non-numeric values.
print(16)
• prints the value 16. Notice that unlike “VKS-Learning Hub” no quotation
• marks (") appear in the statement.
• The value 16 is an example of an integer expression. Python supports other
types of expressions besides integer expressions.
• An expression is part of a statement.
• The number 16 by itself is not a complete Python statement and, therefore,
cannot be a program. The interpreter, however, can evaluate a Python
expression.
• You may type the enter 16 directly into the interactive interpreter shell:
• The interactive shell attempts to evaluate both expressions and statements. In
this case, the expression 16 evaluates to 16.
• The shell executes what is commonly called the read, eval, print loop. This
means the interactive shell’s sole activity consists of
• 1. reading the text entered by the user,
• 2. attempting to evaluate the user’s input in the context of what the user
has entered up that point, and
• 3. printing its evaluation of the user’s input.
• If the user enters a 16 the shell interprets it as a 16.
• If the user enters x = 10, a statement has has no overall value itself, the shell
prints nothing.
• If the user then enters x, the shell prints the evaluation of x, which is 10.
• If the user next enters y, the shell reports a error because y has not been
defined in a previous interaction.
• Python uses the + symbol with integers to perform normal arithemtic addition,
so the interactive shell can serve as a handy adding machine:
Python recognizes both single quotes (’) and double quotes (") as valid ways to delimit a string value.
If a single quote marks the beginning of a string value, a single quote must delimit the end of the string.
Similarly, the double quotes, if used instead, must appear in pairs.
You may not mix the quotes when representing a string:
All expressions in Python have a type.
The type of an expression indicates the kind of expression it is.
An expression’s type is sometimes denoted as its class.
The built in type function reveals the type of any Python expression:
Constants
• Fixed values such as numbers, letters, and strings
are called “constants” - because their value does
not change
• Numeric constants are as you expect
• String constants use single-quotes (')
or double-quotes (")
Variables
• A variable is a named place in the memory where a
programmer can store data and later retrieve the data using the
variable “name”
• Programmers get to choose the names of the variables
• You can change the contents of a variable in a later statement
12.2x
14y
x = 12.2
y = 14
100
x = 100
Python Variable Name Rules
• Must start with a letter or underscore _
• Must consist of letters and numbers and
underscores
• Case Sensitive
• Good: spam eggs spam23 _speed
• Bad: 23spam #sign var.12
• Different: spam Spam SPAM
Reserved Words
• You can not use reserved words as variable names
/ identifiers
and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print
x = 10
This is an assignment statement.
An assignment statement associates a value with a variable.
The key to an assignment statement is the symbol = which is known as the assignment
operator.
The statement assigns the integer value 10 to the variable x. Said another way, this
statement binds the variable named x to the value 10.
A variable may be assigned and reassigned as often as necessary. The type of a variable will
change if it is reassigned an expression of a different type.
• print(x)
This statement prints the variable x’s current value.
Note that the lack of quotation marks here is very important. If x has the value 10, the
statement
print(x)
prints 10, the value of the variable x, but the statement
print('x')
prints x, the message containing the single letter x.
Sentences or Lines
x = 2
x = x + 2
print x
Variable Operator Constant Reserved Word
Assignment Statement
Assignment with expression
Print statement
Assignment Statements
• We assign a value to a variable using the assignment
statement (=)
• An assignment statement consists of an expression on
the right hand side and a variable to store the result
Right side is an expression. Once expression is evaluated, the result is placed in (assigned to) x.
A variable is a memory location used to store a value. The
value stored in a variable can be updated by replacing the old
value (10) with a new value (220).
x = 2 * x * ( 1 + x )
10 220x
Right side is an expression.
Once expression is evaluated,
the result is placed in (assigned
to) the variable on the left side
(i.e. x).
220
A variable is a memory location
used to store a value. The
value stored in a variable can be
updated by replacing the old
value (10) with a new value
(220).
Numeric Expressions
• Because of the lack of
mathematical symbols on
computer keyboards - we use
“computer-speak” to express
the classic math operations
• Asterisk is multiplication
• Exponentiation (raise to a
power) looks different from in
math.
Operator Operation
+ Addition
- Subtraction
* Multiplication
/ Division
** Power
% Remainder
Numeric Expressions
Operator Operation
+ Addition
- Subtraction
*
Multiplicatio
n
/ Division
** Power
% Remainder
Order of Evaluation
• When we string operators together - Python must
know which one to do first
• This is called “operator precedence”
• Which operator “takes precedence” over the others
29
Order of Operations
Operator Operation Precedence
() parentheses 0
** exponentiation 1
* multiplication 2
/ division 2
// int division 2
% remainder 2
+ addition 3
- subtraction 3
Operator Precedence Rules
• Highest precedence rule to lowest precedence
rule
• Parenthesis are always respected
• Exponentiation (raise to a power)
• Multiplication, Division, and Remainder
• Addition and Subtraction
• Left to right
31
The computer scans the expression from
left to right,
first clearing parentheses,
second, evaluating exponentiations from left to right
in the order they are encountered
third, evaluating *, /, //, % from left to right
in the order they are encountered,
fourth, evaluating +, - from left to right
in the order they are encountered
Parenthesis
Power
Multiplication Division Modulus
Addition Subtraction
Left to Right
10 + 2 * 3 -50/ 5* *2
10 + 2 * 3 - 50/25
10 + 6 - 50/25
10 + 6 - 2
16-2=14
Comments in Python
• Anything after a # is ignored by Python
• Why comment?
• Describe what is going to happen in a sequence of code
• Document who wrote the code or other ancillary
information
• Turn off a line of code - perhaps temporarily
34
function use
Str() Converts a number to
a string
int() Converts an object to an
integer and truncates.
round() Converts an object to an
integer and rounds.
float Converts an object to a
float.
Vks python
Vks python
Vks python
Vks python
Vks python
Vks python
Vks python

Vks python

  • 1.
    What is Python…? •Python is a general purpose programming language that is often applied in scripting roles. • So, Python is programming language as well as scripting language. • Python is also called as Interpreted language Differences between program and scripting language a program is executed (i.e. the source is first compiled, and the result of that compilation is expected) • A "program" in general, is a sequence of instructions written so that a computer can perform certain task. a script is interpreted • A "script" is code written in a scripting language. A scripting language is nothing but a type of programming language in which we can write code to control another software application.
  • 2.
    What can Ido with Python…? • System programming • Graphical User Interface Programming • Internet Scripting • Component Integration • Database Programming • Gaming, Images, XML , Robot and more 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
  • 3.
    Why do peopleuse 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 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.
  • 5.
    • Python programsmust be written with a particular structure. The syntax must be correct, or the interpreter will generate error messages and not execute the program. For example print(“VKS-Learning Hub“ ) We will consider two ways in which we can run this statement • 1. enter the program directly into IDLE’s interactive shell and • 2. enter the program into IDLE’s editor, save it, and run it.
  • 6.
    • IDLE’s interactiveshell. IDLE is a simple Python integrated development environment available for Windows, Linux, and Mac OS X. To start IDLE from the Microsoft Windows Start menu. The IDLE interactive shell will open with >>> prompt. You may type the above one line Python program directly into IDLE and press enter to execute the program. the result will be display using the IDLE interactive shell.
  • 7.
    Since it doesnot provide a way to save the code you enter, the interactive shell is not the best tool for writing larger programs. The IDLE interactive shell is useful for experimenting with small snippets of Python code IDLE’s editor. IDLE has a built in editor. From the IDLE menu, select New Window, Editor will open a file . Type the text print(“Faips Kuwait”) into the editor. You can save your program using the Save option in the File menu as shown in Figure. Save the code to a file named try1.py. The extension .py is the extension used for Python source code. We can run the program from within the IDLE editor by pressing the F5 function key or from the editor’s Run menu: Run→Run Module. The output appears in the IDLE interactive shell window.
  • 8.
    print(“VKS-Learning Hub") This isa Python statement. A statement is a command that the interpreter executes. This statement prints the message VKS-Learning Hub on the screen. A statement is the fundamental unit of execution in a Python program. Statements may be grouped into larger chunks called blocks, and blocks can make up more complex statements. Higher-order constructs such as functions and methods are composed of blocks. The statement print(“VKS-Learning Hub") makes use of a built in function named print
  • 9.
    If you tryto enter each line one at a time into the IDLE interactive shell, the program’s output will be intermingled with the statements you type. In this case the best approach is to type the program into an editor, save the code you type to a file, and then execute the program. Most of the time we use an editor to enter and run our Python programs. The interactive interpreter is most useful for experimenting with small snippets of Python code.
  • 10.
    It is importantthat no whitespace (spaces or tabs) come before the beginning of each statement. In Python the indentation of statements is significant and must be done properly. If we try to put a single space before a statement in the interactive shell The interpreter reports a similar error when we attempt to run a saved Python program if the code contains such extraneous indentation.
  • 11.
  • 12.
    Python supports anumber of numeric and non-numeric values.
  • 13.
    print(16) • prints thevalue 16. Notice that unlike “VKS-Learning Hub” no quotation • marks (") appear in the statement. • The value 16 is an example of an integer expression. Python supports other types of expressions besides integer expressions. • An expression is part of a statement. • The number 16 by itself is not a complete Python statement and, therefore, cannot be a program. The interpreter, however, can evaluate a Python expression. • You may type the enter 16 directly into the interactive interpreter shell:
  • 14.
    • The interactiveshell attempts to evaluate both expressions and statements. In this case, the expression 16 evaluates to 16. • The shell executes what is commonly called the read, eval, print loop. This means the interactive shell’s sole activity consists of • 1. reading the text entered by the user, • 2. attempting to evaluate the user’s input in the context of what the user has entered up that point, and • 3. printing its evaluation of the user’s input. • If the user enters a 16 the shell interprets it as a 16. • If the user enters x = 10, a statement has has no overall value itself, the shell prints nothing. • If the user then enters x, the shell prints the evaluation of x, which is 10. • If the user next enters y, the shell reports a error because y has not been defined in a previous interaction. • Python uses the + symbol with integers to perform normal arithemtic addition, so the interactive shell can serve as a handy adding machine:
  • 15.
    Python recognizes bothsingle quotes (’) and double quotes (") as valid ways to delimit a string value. If a single quote marks the beginning of a string value, a single quote must delimit the end of the string. Similarly, the double quotes, if used instead, must appear in pairs. You may not mix the quotes when representing a string:
  • 16.
    All expressions inPython have a type. The type of an expression indicates the kind of expression it is. An expression’s type is sometimes denoted as its class. The built in type function reveals the type of any Python expression:
  • 17.
    Constants • Fixed valuessuch as numbers, letters, and strings are called “constants” - because their value does not change • Numeric constants are as you expect • String constants use single-quotes (') or double-quotes (")
  • 18.
    Variables • A variableis a named place in the memory where a programmer can store data and later retrieve the data using the variable “name” • Programmers get to choose the names of the variables • You can change the contents of a variable in a later statement 12.2x 14y x = 12.2 y = 14 100 x = 100
  • 19.
    Python Variable NameRules • Must start with a letter or underscore _ • Must consist of letters and numbers and underscores • Case Sensitive • Good: spam eggs spam23 _speed • Bad: 23spam #sign var.12 • Different: spam Spam SPAM
  • 20.
    Reserved Words • Youcan not use reserved words as variable names / identifiers and del for is raise assert elif from lambda return break else global not try class except if or while continue exec import pass yield def finally in print
  • 21.
    x = 10 Thisis an assignment statement. An assignment statement associates a value with a variable. The key to an assignment statement is the symbol = which is known as the assignment operator. The statement assigns the integer value 10 to the variable x. Said another way, this statement binds the variable named x to the value 10. A variable may be assigned and reassigned as often as necessary. The type of a variable will change if it is reassigned an expression of a different type. • print(x) This statement prints the variable x’s current value. Note that the lack of quotation marks here is very important. If x has the value 10, the statement print(x) prints 10, the value of the variable x, but the statement print('x') prints x, the message containing the single letter x.
  • 23.
    Sentences or Lines x= 2 x = x + 2 print x Variable Operator Constant Reserved Word Assignment Statement Assignment with expression Print statement
  • 24.
    Assignment Statements • Weassign a value to a variable using the assignment statement (=) • An assignment statement consists of an expression on the right hand side and a variable to store the result Right side is an expression. Once expression is evaluated, the result is placed in (assigned to) x. A variable is a memory location used to store a value. The value stored in a variable can be updated by replacing the old value (10) with a new value (220).
  • 25.
    x = 2* x * ( 1 + x ) 10 220x Right side is an expression. Once expression is evaluated, the result is placed in (assigned to) the variable on the left side (i.e. x). 220 A variable is a memory location used to store a value. The value stored in a variable can be updated by replacing the old value (10) with a new value (220).
  • 26.
    Numeric Expressions • Becauseof the lack of mathematical symbols on computer keyboards - we use “computer-speak” to express the classic math operations • Asterisk is multiplication • Exponentiation (raise to a power) looks different from in math. Operator Operation + Addition - Subtraction * Multiplication / Division ** Power % Remainder
  • 27.
    Numeric Expressions Operator Operation +Addition - Subtraction * Multiplicatio n / Division ** Power % Remainder
  • 28.
    Order of Evaluation •When we string operators together - Python must know which one to do first • This is called “operator precedence” • Which operator “takes precedence” over the others
  • 29.
    29 Order of Operations OperatorOperation Precedence () parentheses 0 ** exponentiation 1 * multiplication 2 / division 2 // int division 2 % remainder 2 + addition 3 - subtraction 3
  • 30.
    Operator Precedence Rules •Highest precedence rule to lowest precedence rule • Parenthesis are always respected • Exponentiation (raise to a power) • Multiplication, Division, and Remainder • Addition and Subtraction • Left to right
  • 31.
    31 The computer scansthe expression from left to right, first clearing parentheses, second, evaluating exponentiations from left to right in the order they are encountered third, evaluating *, /, //, % from left to right in the order they are encountered, fourth, evaluating +, - from left to right in the order they are encountered
  • 32.
    Parenthesis Power Multiplication Division Modulus AdditionSubtraction Left to Right 10 + 2 * 3 -50/ 5* *2 10 + 2 * 3 - 50/25 10 + 6 - 50/25 10 + 6 - 2 16-2=14
  • 33.
    Comments in Python •Anything after a # is ignored by Python • Why comment? • Describe what is going to happen in a sequence of code • Document who wrote the code or other ancillary information • Turn off a line of code - perhaps temporarily
  • 34.
    34 function use Str() Convertsa number to a string int() Converts an object to an integer and truncates. round() Converts an object to an integer and rounds. float Converts an object to a float.

Editor's Notes

  • #21 Like a dog .... food ... food ...