Python Worksheet 1
Python Worksheet 1
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
Rate_2 Correct
_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
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
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
isdigit
Lower
Upper
Isupper
Islower
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.
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.
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