Вы находитесь на странице: 1из 10

PYTHON CLASS 11

Spructure Spructure

WORKSHEET-1
Explain the following
Token Building block of programming language is known as Token. Python has
following tokens
Example 1 Keywords 2 identifier 3 operator
4 literals 5. Comments 6 delimiters
Keywords 1 Reserved words having special meaning of python interpreter
2 cannot be redefined . Interpreter has list of keywords stored in it
Examples 1 if 2 else 3 while 4 for 5 import 6
Identifier 1 User defined or built in name for variable function class.
2 can be redefined.
Examples of built in 1 2 3 4 5
Operator Operators are special symbols in Python that carry out arithmetic or logical computation. The
value that the operator operates on is called the operand.

Types of Operator
1. Arithmetic Examples + , - , *, /, //, %
2. Relational Examples > ,<, >=, <=, ==, !=
3. Logical Examples and, or, not
4. Assignment Examples =,+=,-=,*=,/=,%=,

Comment Non executable lines which are ignored by complier are called coments

In Python comments is given by putting # before comment line

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 1


PYTHON CLASS 11
Spructure Spructure
Variable variable is a named location used to store data in the memory. Each variable must
have a unique name called identifier

Rules for declaring variable


1 Variable name must start with alphabet or _
2 variable name should not be keywords
3 Variable name should not contain any special character other than _
Only alphabet , number or _ allowed not even space
4 Variable name are case sensitive
Identify incorrect variable (identifier) and give reason
$rate Incorrect as $ is used. should not contain any special character and
must start with alphabet
Rate_! Incorrect as it contain special character !

Rate_2 Correct

12Rate Incorrect as starting with number

Item Rate Incorrect as contain space

while Incorrect as while is keyword

_Rate correct

Which built-in function is used to return the data type explain with example
type() type function is used to find the data type of variable or expression
>>> a=10 >>> a=’vks’ >>> a=10.5 >>> a=10%3 >>>a = True
>>> type(a) >>>type(a) >>>type(a) >>>type(a) >>>type(a)
<class 'int'> <class 'str'> <class 'float'> <class 'int'> <class 'bool'>
Identify the datatype of following
“123-xy” str ‘vks-learning’ str 10%3 int
123 int 123.67 float 10/3 float
Explain the following built-in function with example
pow The pow() method returns x to the power of y. If the third argument (z) is
given, it returns x to the power of y modulus z, i.e. pow(x, y) % z.
>>> pow(2,3) # output 8
>>> pow(2,3,5) # output 3

sqrt Sqrt is math module function so you have to import math before using it
It returns the square root of number
>>> import math
>>> math.sqrt(100)
10.0

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 2


PYTHON CLASS 11
Spructure Spructure
abs The abs() method returns the absolute value of the given number. If the
number is a complex number, abs() returns its magnitude.
>>> abs(-100) # output 100
>>> abs(4+3j) # output 5.0
max The max() method returns the largest element in an iterable or
largest of two or more parameters.
>> max(23,7,33,5) #output 33
min The min() method returns the smallest element in an iterable or
largest of two or more parameters.
>> min(23,7,33,5) #output 5

len The len() function returns the number of items of an object. Or length of string
>>> len("VKS-LEARNING") #output 12 string contain 12 characters
>>> len([2,3,4,5]) #output 4 as list contain 4 element
round The round() method returns the floating point number rounded off to the
given ndigits digits after the decimal point. If no ndigits is provided, it
rounds off the number to the nearest integer.
>>> round(10.756,1) #output 10.8
>>> round(10.756) #output 11
>>> round(10.34) #output 10
Give the output x,y,z=10,4,12 x,y,z=5,4,3 a,b,c=4,5,3
x,y,z=5,3,2 x+=y+z x+=y+z a+=a+b+c;
x+=y; y*=x+z y+=x+z b+=a+b+c;
y+=x+z; z-=y-x
z*=y-x c+=a+b+c;
z+=x+y+z; print(x,y,z)
print(x,y,z) output print(x,y,z) print(a,b,c)
output 26 152 -114 x=y+z-x a=b+c-a
8 13 25 y//=x-y b*=a-b
z+=2*x+3*y c+=a+2*b
print(x,y,z) print(a,b,c)
Output output
12 19 21 16 29 51
28 2 83 64 1015 2145
Write C++ logical expressions for the following (without using any built-in functions):
To check that an integer variable digit contains single digit
Ans digit>= 0 and digit<=9
To check that a character variable ch contains a lowercase
Ans ch>=’a’ and ch<=’z’
To check that variable num is divisible by 3 but not divisible by 5
Ans num%3==0 and num%5!=0
To check that a variable num is even no and multiple of 4
Ans num%2==0 and num%4==0
To check that a variable year is a leap year
Ans year%4==0 and year%100!==0 or year%400==0

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 3


PYTHON CLASS 11
Spructure Spructure
To check that an integer variable roll contains double digit
Ans roll>=10 and roll<=99
To check that an integer variable year is divisible by 4 but not divisible by 100
Ans year%4==0 and year%100!=0
With suitable examples differentiate between syntax error and run-time error.
Syntax Error Run Time Error
1 Syntax errors happen when the interpreter Syntactically correct statement performs illegal
find something not compelling with Python's operation during execution of a program is called
syntax. Run-Time errors. Illegal operation is performed
when the program encounters unexpected data.
2.These error are catch by python interpreter These error are not catch by python interpreter
and program will not run and give error and program will run but halt at run time due to
message unexpected data or operation
Example Example
 Division by zero (0)
 Typographical mistakes
 Logarithm of zero (0) or negative number
 References to undeclared variables
 Square root of negative no.
 Wrong number or type of parameters
passed to a function
 Call to undefined function >>>10/0
>>>Print(“Hello”) ZeroDivisionError: division by zero
# Print is not built in function it has to be import math
print(“hello”) >>> math.sqrt(-16)
>>> print “hello” # ( ) missing with print ValueError: math domain error
print(a) # a undefined
a=int(input("Input value 1? ")) Identify the following
b=int(input("Input value 2? ")) 2 arithmetic operator +,/,-,*
am =(a + b)/2 2 relational operator > ,!=
1 logical operator and
hm =2*a*b/(a + b);
2 strings “input a value 1?”
print("AM=",am);
“input a value 2?”
print("HM=",hm);
if(a>b): 2 user identifier a,b,am,hm
print("a is greater than b by",a-b ) 2 keywords if, else, elif
elif(b>a): 2 built-in identifier int,input,print
print("b is greater than a by",b-a ) 2 built-in functions print(),int(),input()
else:
print("a nd b are equal")
if(a%2==0 and b%2==0):
print("a and b are EVEN Nos" )
elif(a%2==0 and b%2!=0):
print("a is EVEN but b is ODD" )
else:
print("a and b are ODD Nos")

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 4


PYTHON CLASS 11
Spructure Spructure
Explain Type Casting (Type Conversion )
Converting one type of data type to another type is know as type casting or type conversion
Type of Type Casting
1. Implicit 2. Explicit
Implicit Type casting Explicit Type casting

 Implicit Type Conversion is automatically  Explicit Type Conversion is also called Type
performed by the Python interpreter. Casting, the data types of object are
 Python avoids the loss of data in Implicit converted using predefined function by user.
Type Conversion.  In Type Casting loss of data may occur as we
enforce the object to specific data type.
Example Example

Explain the following function with example


isalpha

isdigit

Lower

Upper

Isupper

Islower

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 5


PYTHON CLASS 11
Spructure Spructure

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 6


PYTHON CLASS 11
Spructure Spructure
Write a Program to input marks of English, Math WAP to print the Discount in Rupees for a
and Science and allocate stream accordingly salesman. The Discount is based on the
following conditions
English, Maths and Science Engineering Sales Dis %
>=80% below Rs. 5000 5%
English and Science Medical Rs. 5000 to Rs. 10000 8%
>=80%,
Maths >=60%
Above Rs. 10000 10%
English, Maths and Science Commerce
>=80%

WAP to input the no of adult and no of children


and distance to calculate the total fare as per
the slab given below

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 7


PYTHON CLASS 11
Spructure Spructure

WAP to calculate the monthly bill. Input a character from a keyboard. Display the
Local calls are charged according to the table inputted character and character type
(Uppercase, Lowercase, Digit and Special
Local character) and its ascii value on the screen.
Charges for Local Calls
Calls Do not use any built-in functions.
1 – 100 No charge
If it is lowercase convert to uppercase after
Rs. 3.00 per call + Rs. 10.00 as
101 – 250 displaying original
surcharge for example, character inputted is ‘a’
Rs. 4.00 per call + Rs. 25.00 as output will be
251 – 500
surcharge ‘a’ is lowercase character its ascii value is 97 Its
501 and Rs. 5.00 per call + Rs. 75.00 as uppercase conversion is ‘A’
above surcharge If uppercase convert to lowercase
Monthly Phone Rent is Rs. 250, for International for example, character inputted is ‘A’
Calls charge is Rs. 50 per call. Total Amount Due output will be
is calculated as Monthly Phone Rent + Charges ‘A’ is uppercase character its ascii value is 65 Its
for Local calls + Charges for International calls. lowercase conversion is ‘a’
Input number of local calls (integer value) and
number of international calls (integer value) is
made in a month. Calculate total amount due and
display the result on the screen.

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 8


PYTHON CLASS 11
Spructure Spructure

WAP program to calculate the Volume of cone WAP program to calculate the total cost of
and cylinder having same radius and height, painting a frustum which is open from both end
radius and height is inputted by user. @ rs 10 per cm2
Input the radius of both ends as r1 and r2 and
height as h1 in cm. Also calculate the volume if
bigger end is closed.

Find & underline error & rewrite correct


code

import mathematics
a=int(input("Input No? ")
n=math.Pow(a,3)
print n;
a,b,c=42,30,
If a>b:
c==a-b
elseif b>a:
c=b-a

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page 9


PYTHON CLASS 11
else
Spructure Spructure
c=b;
print(c)

vinodsrivastava.com Vinsri76@yahoo.com +965-69300304 Page


10

Вам также может понравиться