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

PWP Unit 1 notes

The document provides an overview of Python programming, detailing its features, uses, history, and future prospects. It covers key concepts such as identifiers, variables, data types, input operations, comments, indentation, and various types of operators. Additionally, it explains expressions and the order of execution in Python programming.

Uploaded by

gangurdekasturi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

PWP Unit 1 notes

The document provides an overview of Python programming, detailing its features, uses, history, and future prospects. It covers key concepts such as identifiers, variables, data types, input operations, comments, indentation, and various types of operators. Additionally, it explains expressions and the order of execution in Python programming.

Uploaded by

gangurdekasturi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Unit-I

Introduction and Syntax of Python Program

Q. Explain Features and uses of Python?


Ans:
Features of Python:
 It is free and open source programming Language.
 It is high level programming language.
 It is simple and easy to learn.
 It is portable.
 The python programs do not require compilation, rather they are interpreted.
 It is an object oriented programming language.
 It can be embedded within your C or C++ programs.
 It is rich set of functionality available in its huge standard library
 It has powerful set of built-in data types and easy to use control constructs.
 It has rich and supportive community to help the developer while creating new
applications.
 Using few lines of code many useful applications can be developed using python.
Uses of Python in Industry:
 Top companies are using python as their business application development.
 Central Intelligence Agency(CIA) is using python to maintain their websites.
 Google’s First search engine was written in Python. It was developed in the late 90s.
 Facebook uses the python language in their Production Engineering.
 NASA uses Workflow Automation Tool which is written in python.
 NOKIA which is a popular telecommunication company who uses python for its platform
such as S60.
 Quora is a popular social commenting website which is also written in python.
 The entire stack of Dropbox was written in python.
 Instagram uses python for its Front-end.

1
 You Tube also uses scripted python for their websites

Q. Explain History and Future of Python?


Ans:
History of Python:
 Python is general purpose programming language.
 It is high level and object oriented programming language.
 It was created by Guido van Rossum during 1985-1990
 Python 3.0 was released in 2008. it was incompatibles to all backward versions later on
many of its important features have been back ported to be compatible with version 2.7
Future of Python:
 1. Artificial Intelligence: reduce human efforts with increased accuracy and efficiency for
various development purposes.
 2.Big Data: Analyzing a large number of data sets across computer clusters.
 3 Networking: read, write and configure routers and switches and performing other
networking automation tasks.

Q. What is Python? Explain Characteristics of python.


Ans:
Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming
language. It was created by Guido van Rossum during 1985- 1990. Python is designed to be highly
readable. It uses English keywords frequently where as other languages use punctuation, and it has
fewer syntactical constructions than other languages.

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 Interpreted − Python is processed at runtime by the interpreter. You do not


need to compile your program before executing it. This is similar to PERL and PHP.

 Python is Interactive − You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.

 Python is Object-Oriented − Python supports Object-Oriented style or technique of


programming that encapsulates code within objects.

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

Following are important characteristics of Python Programming −


 It supports functional and structured programming methods as well as OOP.

 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 supports automatic garbage collection.

 It can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.

Q. What is Identifiers? What are conventions for python identifiers?


Ans:
 A Python identifier is a name used to identify a variable, function, class, module or other
object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by
zero or more letters, underscores and digits (0 to 9).

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

Here are naming conventions for Python identifiers −


 Class names start with an uppercase letter. All other identifiers start with a lowercase letter.
 Starting an identifier with a single leading underscore indicates that the identifier is private.
 Starting an identifier with two leading underscores indicates a strongly private identifier.
 If the identifier also ends with two trailing underscores, the identifier is a language-defined
special name.
Example:
Correct identifiers: sum, _area_of_circle, average_of_marks, x, number1 etc.
Incorrect Identifiers: 1number (because it begins with number), case (because it is
keyword), total _of a&b (because & symbols is not allowed in identifiers)

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

Rules for Variable name:

A variable can have a short name (like x and y) or a more descriptive name (age, carname,
total_volume). Rules for Python variables:

 A variable name must start with a letter or the underscore character


 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9,
and _ )
 Variable names are case-sensitive (age, Age and AGE are three different variables)

4
Q. What is Data Types? Explain different data types supported by python.
Ans:

Data types are used to define type of variable.


In python there are five types of data types.
1. Numeric
2. String
3. List
4. Tuple
5. Dictionary

1. Numeric: It is used to hold numeric values.


a. int- holds signed inetgers of on-limited length
b. long- holds long integers.
c. Float- holds float pint numbers
d. Complex- holds complex numbers.

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

Where prompt is the string we wish to display on the screen. It is optional.

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

Q. Explain the types of operators used in python.


Ans:
o Operator: It is special symbol that are used in computations.
o Operands: The values that operator uses for performing computations are called
operands.
Various operator are used in python are
1. Arithmetic operators: used for performing arithmetic operations.

operator Meaning Example


+ Addition operator Used for performing 10+20=30
addition of numbers
- Subtraction operator Used for performing 10-20=-10
subtraction of numbers
* Multiplication operator Used for performing 10*20=200
multiplication of numbers
/ Division operator Used for performing 10/20=0.5
division of numbers
% Mod operators gives the remainder value 10%2=0
** This is an exponentiation operator(Power) 2**3=8
// This Floor division operator. In this 10//3=3
operation the result is the quotient in which
the digits after the decimal point are removed

2. Comparison Operators: It compare the values and establish the relationship


among them.
Operator Meaning
== If two values are equal then condition becomes true

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:

Operator Meaning Example

9
and If both the operand are true then the entire expression is true a and b

or If either first or second operand is true a or 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

Operator Meaning Example if a=010, b=011

This is bitwise and operator a&b=010


&
This is bitwise or operator a|b=011
|
This is bitwise not operator ~a=101
~
The binary xor operation will always a xor b=001
xor
produce a 1 output if either of its input is 1
and will produce a 0 output if both of its
input are 0 and 1

10
The left shit operator a<<1 means make left shift by
<<
one position and add a tailing
zero .i.e.100

The right shift operator a>>1 means make right shift


>>
by one position and add
leading zero i.e.001

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

in It returns true if a sequence with specified value is present in the


object.

not in It returns true if a sequence with specified value is not present in the
object.

Example:

>>> color=["red","blue","black","yellow"]

>>> print("blue" in color)

True

11
>>> print("white" in color)

False

>>> print("white" not in color)

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

Q. what are the types of Expressions? Explain

Ans:

o Expression is a combination of values, variables and operators.


o Value is considered as an expression.

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

Rule of precedence in python is PEMDAS

1. P: Parenthesis have the highest precedence, so expressions in parenthesis are evaluated


first,(1*(10-5)=5

2. E: Exponentiation has the next highest 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:

1. Infix Expression: It is type of expression in which, expression is written in the form as

“Operand1 operator operand2”

Example: a + b

2. Prefix Expression: It is type of expression in which, expression is written in the form


as

13
“Operator Operand1 operand2”

Example: +a b

3. Postfix Expression: It is type of expression in which, expression is written in the form


as

“Operand1 operand2 operator”

Example: ab+

Python Download and Installation Instructions


First, Download the latest version of Python from the official website https://www.python.org

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.

The file should appear as

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.

• Click the Close button.

18
• Python should now be installed.

Modes of Working in Python


• Python has two basic modes: 1. Interactive Mode 2.Script mode

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.

• Step 1: Open python shell by clicking the python IDLE.

• Step 2: On file menu click on New File Option.

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.

• Step 6 : The Output will be displayed on the python shell.

20
21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy