PWP Unit 1 notes
PWP Unit 1 notes
1
You Tube also uses scripted python for their websites
Python is a MUST for students and working professionals to become a great Software Engineer
especially when they are working in Web Development Domain. I will list down some of the key
advantages of learning Python:
Python is Interactive − You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
2
Python is a Beginner's Language − Python is a great language for the beginner-level
programmers and supports the development of a wide range of applications from simple
text processing to WWW browsers to games.
Characteristics of Python
It can be used as a scripting language or can be compiled to byte-code for building large
applications.
It provides very high-level dynamic data types and supports dynamic type checking.
It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
Python does not allow punctuation characters such as @, $, and % within identifiers.
Python is a case sensitive programming language. Thus, Manpower and manpower are
two different identifiers in Python.
3
Q. What is Variable? State rules for variable name.
Ans:
Variables are nothing but reserved memory locations to store values.
This means that when you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what
can be stored in the reserved memory.
Python variables do not need explicit declaration to reserve memory space.
The declaration happens automatically when you assign a value to a variable.
The equal sign (=) is used to assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the
right of the = operator is the value stored in the variable.
Example:
counter = 100 # An integer assignment
miles = 1000.0 # A floating point
name = "John" # A string
print counter
print miles
print name
Here, 100, 1000.0 and "John" are the values assigned to counter, miles, and name variables,
respectively. This produces the following result −
100
1000.0
John
A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:
4
Q. What is Data Types? Explain different data types supported by python.
Ans:
Example:
a=10 #int
x = 1.10 #float
x = 3+5j #complex
2. String: It is collection of characters.
We can use single quote, double quote or triple quote to define string.
Example:
>>> str1="my"
>>> str2="python"
>>> print(str1)
my
>>> print(str2)
Python
3. List: It is basically an ordered sequence of some data written using square brackets [] and
commas (,).
Example:
5
>>> a=[10,20,30,40,50]
>>> print(a)
[10, 20, 30, 40, 50]
>>> b=['a','b','c']
>>> print(b)
['a', 'b', 'c']
4. Tuple: It is collection of elements and it is similar to lists. Items of list are separated by
commas and elements are enclosed in () parenthesis.
Example:
>>> a=(10,20,30,40,50)
>>> print(a)
(10, 20, 30, 40, 50)
>>> b=('a','b','c')
>>> print(b)
('a', 'b', 'c')
5. Dictionary: It is collection of elements in the form of key:value pair. The elements of
dictionary are present in the curly brackets {}.
Example:
>>> a={1:'red',2:'blue',3:'black'}
>>> print(a)
{1: 'red', 2: 'blue', 3: 'black'}
>>> print(a.keys())
dict_keys([1, 2, 3])
>>> print(a.values())
dict_values(['red', 'blue', 'black'])
Q. Input Operations:
o In python it is possible to input the data using keyboard.
o For that purpose, the function input() used.
o Syntax:
input([prompt])
6
Example:
Print(“Enter the number:”)
a=input()
Q. Comments
o It is kind of statement that are written in the program for program understanding
purpose.
o It is possible to understand what exactly the program is doing.
o In python we use hash(#)symbol for comment.
o It extends up to newline character.
o Python interpreter ignores comment
o Example:
#this is comment line
a=10 #int variable
o If we want to give comment to multiple lines, use # at the beginning of each line.
Example:
#this is
#another example
#of comment statement
Q. Indentation:
o Leading white space at the beginning of the logical line is called indentation.
o Python programs get structured through indentation.
o Code blocks are defined by their indentation.
o All statements with the same distance to the right belong to the same block of code
i.e. the statements within a block line up vertically.
o If a block has to be more deeply nested, it is simply indented further to the right.
o Example:
print(“Enter the age:”)
age=int(input())
if age>18:
print(“Voting right”)
7
else:
print(“ No Voting right”)
8
!= If two values are not equal then the condition becomes
true
<> Similar to !=. that means If two values are not equal then
the condition becomes true
< Less than operator. If left operand is less than the right
operand then return value is true
> Greater than operator. If left operand is greater than the
right operand then return value is true
<= Less than equal to operator. If left operand is less than
the right operand or equal to right operand then return
value is true
>= Greater than equal to operator. If left operand is greater
than the right operand or equal to right operand then
return value is true
>>> 10<20
True
>>> 10<=20
True
>>> 20>10
True
>>> 20>=10
True
>>> 20==20
True
>>> 20!=10
True
3. Logical Operator:
9
and If both the operand are true then the entire expression is true a and b
not If the operand is false then the entire expression is true not a
>>> a=True
>>> b=False
>>> a and b
False
>>> a or b
True
>>> not a
False
1. Bitwise operators: work on the bits of the given value. These bits are binary numbers i.e.
0 or 1
Example: 2=010
3=011
10
The left shit operator a<<1 means make left shift by
<<
one position and add a tailing
zero .i.e.100
2. Assignment operators:
It is used to assign values to the variables.
= This is an Assignment operator
Example: a=5
a=a+5
3. Membership Operators:
There are two types of membership operators- in and not in
These are used to find out whether a value is a member of a sequence such
as string or list.
Operator meaning
not in It returns true if a sequence with specified value is not present in the
object.
Example:
>>> color=["red","blue","black","yellow"]
True
11
>>> print("white" in color)
False
True
4. Identity Operators:
There are two identity operators. is and is not
The ‘is’ operator returns true if both operand point to same memory
location.
The ‘is not’ operator returns true if both the operand point to different
memory location.
Example:
>>> color1=["red","blue","green"]
>>> color2=["red","blue","green"]
>>> color3=color1
>>> print(color1 is color2)
False
>>> print(color1 is color3)
True
>>> print(color1 is not color2)
True
Ans:
>>> a=10
>>> a=a+20
12
>>> a
30
>>> 2+3*4
14
The first statement in above code is assignment statement. The second statement
is an expression. Interpreter evaluates the expression and display the result.
Order of execution:
When more than one operator appears in an expression, the order of evaluation depends
on the rules of precedence.
3. MDAS: Multiplication and Division have the same precedence, which is higher than
addition and subtraction, which also have the same precedence. So 2+3*4= 14 rather than 20 .
4. Operators with the same precedence are evaluated from left to right so 3-2+1=2
Types of Expression:
Example: a + b
13
“Operator Operand1 operand2”
Example: +a b
Example: ab+
Downloading
Click Python Download. The following page will appear in your browser.
Click the Windows link (two lines below the Download Python 3.7.4 button). The following page will
appear in your browser.
14
Click on the Download Windows x86-64 executable installer link under the top-left Stable Releases.
The following pop-up window titled Opening python-3.74-amd64.exe will appear.
15
Click the Save File button.
The file named python-3.7.4-amd64.exe should start downloading into your standard download folder.
This file is about 30 Mb so it might take a while to download fully.
Move this file to a more permanent location, so that you can install Python (and reinstall it easily later, if
necessary
Installing
Double-click the icon labeling the file python-3.7.4-amd64.exe.A Python 3.7.4 (64-bit) Setup pop-up
window will appear.
16
Ensure that the Install launcher for all users (recommended) and the Add Python 3.7 to
PATH checkboxes at the bottom are checked. If the Python Installer finds an earlier version of Python
installed on your computer, the Install Now message may instead appear as Upgrade Now (and the
checkboxes will not appear).
• Highlight the Install Now (or Upgrade Now) message, and then click it. When run, a User
Account Control pop-up window may appear on your screen. it asks, Do you want to allow this
app to make changes to your device.
• Click the Yes button. A new Python 3.7.4 (64-bit) Setup pop-up window will appear with a Setup
Progress message and a progress bar.
17
• During installation, it will show the various components it is installing and move the progress bar
towards completion. Soon, a new Python 3.7.4 (64-bit) Setup pop-up window will appear with
a Setup was successfully message.
18
• Python should now be installed.
1) Interactive Mode: It is a command line shell which gives immediate feedback for each
statement, while running previously fed statements in active memory. As new lines are fed into
the interpreter, the fed program is evaluated both in part and in whole.
• The >>> is python’s way of telling you that you are in interactive mode.
2) Script Mode: This is also called as normal mode. This is mode in which python commands are stored
in a file and the file is saved using the extension .py
• We can write a simple python program in script mode using following steps.
19
• Step 3 : Give some suitable file name with extension .py (I have created test.py)
• Step 4 : A File will get opened and the type some programming code.
• Step 5 : Now run your code by clicking on Run Run Module on menu bar.
20
21