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

VKS Python

Python is a general purpose programming language that can be used for both scripting and programming. It is an interpreted language, meaning code is executed line by line without compiling. Python can be used for tasks like system programming, graphical user interfaces, internet scripting, databases, and more. Many large companies use Python including Google, Intel, YouTube, and ESRI. Python is popular because it is free, powerful, portable, and object-oriented.

Uploaded by

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

VKS Python

Python is a general purpose programming language that can be used for both scripting and programming. It is an interpreted language, meaning code is executed line by line without compiling. Python can be used for tasks like system programming, graphical user interfaces, internet scripting, databases, and more. Many large companies use Python including Google, Intel, YouTube, and ESRI. Python is popular because it is free, powerful, portable, and object-oriented.

Uploaded by

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

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. a script is interpreted
the source is first compiled, • A "script" is code written in
and the result of that compilation is a scripting language.
expected) A scripting language is nothing
• A "program" in general, is a but a type of programming
sequence of instructions language in which we can
written so that a computer write code to control
can perform certain task. 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

x = 12.2 x 12.2 100


y = 14
x = 100 y 14
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 Assignment Statement


x=x+2 Assignment with expression
print x Print statement

Variable Operator Constant Reserved Word


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).
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 )

Right side is an expression.


Once expression is evaluated, 220
the result is placed in (assigned
to) the variable on the left side
(i.e. x).
Numeric Expressions

• Because of the lack of Operator Operation


mathematical symbols on + Addition
computer keyboards - we use
“computer-speak” to express - Subtraction

the classic math operations * Multiplication

• Asterisk is multiplication / Division

• Exponentiation (raise to a ** Power


power) looks different from in % Remainder
math.
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
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

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. 34

You might also like