Computer Applications Arihant
Computer Applications Arihant
09
Python: Programming
Language
Chapter Checklist Revision of Python Basics
Python is an object oriented programming language created by Guido Van Rossum in
oRevision of Python Basics
1989. It is a high level, interpreted and interactive language. Python is ideally designed
Features of Python for rapid prototyping of complex applications. It has interfaces for many operating
Python Character Set system calls and libraries. It is extensible to C or C++. Python is widely used in artificial
Token intelligence, natural language generation, neural networks and other advanced fields of
+latteam tnndopemdenk
176 Allinone Computer Applications Class 10th
Invalid ldentifiers
def return
identifiers, keywords, literals, operators and punctuators.
del import try
also calLd
Vakable) in while
elif
1. ldentifiers with
Identifiers are the fundamental building blocks of a else IS
except lambda yield
program which are used to give the name to data items
include by the programmer. The identifier is a sequence of
characters taken from the Python character set. 3. Literals or Constants
The rules in Python for identifiers are Literals refer to fixed values that the program may not alter
) Only Alphabets, Digits and Underscores are permitted. during the execution. They are used to express particular
Gi) Identifier name cannot start with a digit. values within the Python program.
ii) Keywords cannot be used as identifier. Python allows several kinds of literals
(io) Upper case and lower case letters are distinct because ) String literals i )Numeric literals
Python is case sensitive language. Gi) Boolean literals 1 (iv) Special literal None
) Special characters are not allowed except for () Literal eollectíons
underscore).
e.g.
4. Operators
Valid ldentifiers
Operators those tokens that perform
are a specific task/
ldentifier Explanation computations when applied on variable or some other
Name Capital letter with small letters are allowed objects in an expression (known as operands). Python
carries a wide range of operators which are as follows
name Smallletters are allowed
name 1 Digits and underscore are allowed along with () Arithmetic operators ) Comparison operators
alphabets. Gi) Logical operators (io) Bitwise operators
IN Keywords are allowed but we have to change case of
(Assignmentoperators (vi) Special operator
anyletter or complete word.
Underscore at the first position is allowed (vit) Membership operators
SUM
177
Python: Programming Language
Division Operator /)
This operator is used to divide a number by another
Check Point 02 numbers?
number. 1 Which numbers data type holds floating precision
e.g. If x=6 and y=3 (a) complex (b) int
o loat (d) long
then (/y) gives the result 2. 2 Write the symbol which is used in String for horizontal tab. \E
then (x//y) gives the result 4.0. statements allow the programmer to change the CPU's path
through the program. Every programming language
Exponentiation Operator (**) supports various types of control structures as sequence,
It performs exponentiation (power) calculation. It selection and iteration.
returns the result of a number raised to a power.
eg. If x =2, y =3 (i) Sequence
then (x*"y) gives the result 8. Sequence control means the statements are being executed
sequentially. Sequence structure is also known as straight
Modulus Operator (% line path.
This operator is used to find the remainder when its one
Statement 1
operand is divided by its other operand. Both the
operands must be of integer type.
Statement 2
e.g. If x =9, y=2 Statement 3
then (%y) gives the result 1.
Statement n
2. Unary Operators (i) Selection
Those operators that work on single operand to form an set
When programmers are required to execute a particular
expression are known as unary operators. These are two particular test condition,a
basic unary operators of statements depending upon a
paths in a
allows choosing between two or more execution statement(s)
program. else
Python provides below types of selectors: statements(5)
(i) if statement e.
The simplest and most common selection structure is the if y=3
Check Point 03
1 Which of the following loop tests its condition before executing the
A while loop tests for its ending condition before statements in Python?
executing the statements enclosed in the loop body (a) while (b) for (o) do..while (d) if
even the first time. So if the
ending condition is met 2 Write the syntax of for loop
when the while loop begins, the lines of instructions 3 Draw the flow diagram for if statement.
it contains will never be carried out.
Syntax Programs
while (loop-condition): Program 1 For accepting an integer from user and
compound statement.
print("The factorial of number 1s:",f)
SUMMARY
general-purpose programming
Python is an interpreted high-level programming language for
Python was created by Guido Van Rossum in
1989.
sets.
Python uses the traditional ASCII character
function, class, module or other object.
A Python identifier is a name used to identify a variable,
that cannot be used as ordinary identifiers.
Keywords in Python are reserved words
that carry out arithmetic or logical computation.
Operators are special symbols is Python
to store values.
Variables are nothing but reserved memory locations
being executed sequentially
Sequence control means the
statements are
a test condition.
Iteration means repetition of a set of statements, depending upon
i f statement allows branching depending upon the
value or state of variables.
while loop statement repeatedly executes a target statement as long as a given condition is true
times and abbreviates the code that manages the loop variable.
for loop executes a sequence of
statements multiple
Exam Practice
(Multiple Choice Questions [1 Mark each]
4 Multiline strings can be denoted using triple 9 Iterative statements evaluate to either True or
quotes False.
Ans. True
Muliline strings can be denoted using triple Ans. True Iterative statements enable a program with a cyclic
quotes"or """ flow of logic which evaluates to cither True or False.
Whowas developed the Python language? 7 Which symbol is called the assignment
Ans. Python was developed by Guido Rossum in 1989. operator?
Ans. The symbol = is called the assignment operator.
2 What do you mean by Python character set?
Ans. Character set is a set of valid characters that a language 8 Draw the flow chart of sequence control
can recognise. structure.
Ans.
3 Write a short note on token. Statement1
Ans. A smallest individual unit in a program is known as
token. Statement2
4 Define the concept of constant in Python. Statement 3
Ans. Constants are the data values that can never change
their values during program execution Statement n
5 Write the name of any four punctuators used 9Explain the term selection statement in
in Python program.
Python.
Ans. Brackets 0, Parenthesis 0, Braces {}, Colon: Ans. Selection statement is a control statement that allows
6 How many types of constant are there in choosing berween two or more execution paths in a
program.
Python?
Ans. 10 Write the syntax of if statement.
Python allows five types of constants
String, Numeric, Boolean, Special None and Literal Ans. if (conditional expression):
collection. statements
Python: Programming Language 185
How many loops are there in Python 12 What is the syntax of while
loop in Python?
programming? Ans. while (loop-condition):
Ans. There are two
loops in Python programming as
(G) for loop
(i) while loop
loop-body
>>> city="Meerut"
Ans. Python has the following features >>printa
() It has a large and broad >>print city
library.
(i) It has support for an
interactive mode. Output
ii) Python is an open source sofrware.
10
2 Which of the Mcerut
following are invalid names and
why?
G) PaidInterest (i) S-num 6 Write one example of following terms
(iii) Percent (iv) 123 (1) Identifier (ii) Punctuator
(v) abc km (vi) class (ii) Keyword
Ans. () Valid. Ans. () Identifier Sum_1 (ii) Punctuator Bracket 0
(i) Invalid, () is not allowed in the variable name. i) Keyword import
(ii) Valid. 7 Draw the flow diagram of selection
(iv) Invalid, first character must be alphabet. statements in Python.
Invalid, spacing is not allowed. Ans.
(vi) Invalid, class is a keyword.
3 Differentiate between identifier and
keyword. Condition True Statement 1J
Ans. Differences between identifier and keywords are as
follows False Statement2
Statement 1
Identifier Keywords
ldentifier is a name given to Keywords are predefined Statement1"
a class, interface, function reserved words that have
or variable. special meanings.
Identifier consists any Keyword must consist only
combination of letters
8 Explain the if else statement in Python.
letters
numbers and underscores. Ans. if else statement also tests a conditional expression. If
the condition is true, the entire block of statements
It allows both upper case It allows only lower case
and lower case. except False, None, True. following the if will
be executed, othewise the entire
block ofstatements following the else will be executed.
The purpose of identifier is We cannot use keywords
to provide names to for naming classes, 9Write output ofthe following code.
program elements. functions or variables. X = 4
y = 5
4What do you mean by Punctuators?
if (x> y):
Ans. Refer to text on Page No. 177.
print ("x is greater")
5 How to declare the variable and assigning else
values in Python? print ("y is greater")
Ans.
Pyrhon does not bound us to declare variable variable
before Ans. Output
using in the application. It allows us to create
at
y is greater
required time. The equal () operator is used to assign
value to a variable.
186 Allinone Computer Applications Class 10th
The sum is: 136.40 13 Rewrite the following code after removed all
What will be the output of the following code: syntatical errors and underline each.
X = 2 n-input (1nt("Enter the number"))
sum 0
forx in range (2, 20+2,
+2) i 1
print (x) while 1(=n
Ans. Output
sum + 1 = Sum
2 1 = 1 + 2
print(sum);
6 Ans. Correct code is:
8 n-int input("Enter the number"))
10 sum 0
12 i = 1
14 while i=n:
16 Sum= Sum + 1
18 i= i + 2
20 print (sum)
Write the
program with appropriate output to
enter three numbers print("These angles will not make a
O
Input three angles and determine if
Output
they Enter the number:67
make a triangle or not
Ans. n=67
a=float (input("Enter first angle:")) 67 is a prime number
b-float(input("Enter second angle:"))
ang
c=float(input("Enter third
sum=a+b+C
A >>>