VKS Python
VKS 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
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
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
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).
A variable is a memory location
used to store a value. The
value stored in a variable can be x 10 220
updated by replacing the old
value (10) with a new value
(220).
x=2 * x * ( 1 + x )
+ 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
Order of Operations
Operator Operation Precedence
() parentheses 0
** exponentiation 1
* multiplication 2
/ division 2
// int division 2
% remainder 2
+ addition 3
- subtraction 3 29
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
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
31
10 + 2 * 3 -50/ 5* *2
10 + 2 * 3 - 50/25
10 + 6 - 50/25
Parenthesis
Power
Multiplication Division Modulus 10 + 6 - 2
Addition Subtraction
Left to Right 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
function use