Assignment 2: Fundamentals of Python

  1. Write a command to get the Python version you are using.

  2. What is PVM?

  3. How do you create a .pyc (bytecode) file from a Python source file?

  4. How do you run a Python file from the command prompt or terminal?

  5. “Data types in Python are not keywords.” True or False?

import keyword
print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Assignment 3: Simple Problems on print()

  1. Write a Python script to print MySirG on the screen.

  2. Write a Python script to print MySirG on the first line and Education Services on the second line.

  3. Write a Python script to print “MySirG” on the screen. Remember to print the double quotes.

  4. Write a Python script to print “Teacher’s Day” on the screen. Remember to print both double quotes and the single quote.

  5. Write a Python script to print on the screen.

print("MysirG")
MysirG
print("MySirG\n Education servicse")
MySirG
 Education servicse
print("MysirG")
print("Educational Services")
MysirG
Educational Services
print("Teacher's day")
Teacher's day
print('Teachers\'s day')
Teachers's day
print("\n")

print("\\n")
\n

Assignment 4: Playing with Variables

  1. Write a Python script containing a variable with some integer value and print the value of this variable.

  2. Write a Python script to print the value of a variable. The variable contains your name as data.

  3. Write a Python script to print the values of three variables, each on a new line. All three variables contain integer values.

  4. Create five variables, each containing a different type of data (such as 35, True, “MySirG”, 5.46, 3+4j). Print the values of all variables along with their data types.

  5. Create three variables representing the current date: one for day, one for month and one for year. Display the date in standard form, e.g. 29/11/2022.

x = 10
print(x)
10
x = "siva"
print(x)
siva
 x = 10
 y = 20
 z = 30
 print(x)
 print(y)
 print(z)
10
20
30
x = 35
print(x, type(x))
x = True
print(x, type(x))
x = 5.46
print(x, type(x))
x = "Siva"
print(x, type(x))
x = 3+5j
print(x, type(x))
35 <class 'int'>
True <class 'bool'>
5.46 <class 'float'>
Siva <class 'str'>
(3+5j) <class 'complex'>
day = 31
month = 8
year = 2026
print(day, month, year, sep = "/")
31/8/2026
print(day, month, year, end = "-")
31 8 2026-
print(day,"-",  month, "-", year)
31 - 8 - 2026

Assignment 5: Import and Keywords

  1. Write a Python script to print all the keywords on the screen.

  2. Use the help section in the Python shell to see all the keywords.

  3. Create two Python files A0.py and A1.py. Create a variable in A1.py and assign some value to it. Write a Python script in A0.py to import the A1 module and print the value of the variable created in A1.py.

  4. Out of all the keywords, name those keywords which are used as data.

  5. What is the use of the del keyword?

print(keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
help(keyword)
Help on module keyword:

NAME
    keyword - Keywords (from "Grammar/python.gram")

MODULE REFERENCE
    https://docs.python.org/3.13/library/keyword#module-keyword

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that
    are considered implementation detail and may vary between Python
    implementations.  When in doubt, consult the module reference at the
    location listed above.

DESCRIPTION
    This file is automatically generated; please don't muck it up!

    To update the symbols in this file, 'cd' to the top directory of
    the python source tree and run:

        PYTHONPATH=Tools/peg_generator python3 -m pegen.keywordgen         Grammar/python.gram         Grammar/Tokens         Lib/keyword.py

    Alternatively, you can run 'make regen-keyword'.

FUNCTIONS
    iskeyword = __contains__(object, /) method of builtins.frozenset instance
        x.__contains__(y) <==> y in x.

    issoftkeyword = __contains__(object, /) method of builtins.frozenset instance
        x.__contains__(y) <==> y in x.

DATA
    __all__ = ['iskeyword', 'issoftkeyword', 'kwlist', 'softkwlist']
    kwlist = ['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'aw...
    softkwlist = ['_', 'case', 'match', 'type']

FILE
    /usr/lib/python3.13/keyword.py

dummy = 10
del dummy
dummy
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
/tmp/ipykernel_2056/3381196692.py in <cell line: 0>()
----> 1 dummy

NameError: name 'dummy' is not defined

Assignment 6: Type Conversion

  1. Write a Python script to convert a number into str type.

  2. Write a Python script to print the Unicode of the character ‘m’.

  3. Write a Python script to print the character representation of the given Unicode 100.

  4. Write a Python script to convert str type data into int type. Also describe when a str value cannot be converted into int type.

  5. How do you convert an integer value into a bool value?

x = 100
str(x)
'100'
x
100
x = str(x)
x
'100'
type(x)
str
print(ord('m'))
109
print(ord('n'))
110
print(chr(106))
j
print(chr(100))
d
bool(0)
False
bool(1)
True
bool(-1)
True

Assignment 7: Number System

  1. Write a Python script to print any number and its binary equivalent.

  2. Write a Python script to store binary number 1100101 in a variable and print it in decimal format.

  3. Write a Python script to store hexadecimal number 2F in a variable and print it in octal format.

  4. Write a Python script to store octal number 125 in a variable and print it in binary format.

  5. Write a Python script to add two numbers: 25 (in octal) and 39 (in hexadecimal), and display the result in binary format.

x = 10
print(bin(x))
0b1010
print(hex(x), oct(x))
0xa 0o12
print(0b1010 + 0xa)
20
print(0xa)
10
print(0b1010)
10

Assignment 8: Input from Keyboard

  1. Write a Python script to take your name as input from the user and then print it.

  2. Write a Python script to take two numbers from the user, calculate their sum and display the result.

  3. Write a Python script which takes the radius from the user and displays the area of the circle.

  4. Write a Python script to calculate the square of a number. The number is entered by the user.

  5. Write a Python script which takes a character from the user and displays its Unicode.

x = input('ask name')
print(x)
ask namedog
dog
x = input('enter first number')
y = input('enter second number')
x + y, int(x)+int(y)
enter first number1
enter second number2
('12', 3)
r  = int(input('give the radius'))
print(3.14*r*r)
give the radius2
12.56
r**2
4
x = input('gie a character')
gie a characterm
print(ord(x))
109

Assignment 9: Simple Calculations on User Data

  1. Write a Python script to calculate simple interest.

  2. Write a Python script to calculate the area of a rectangle.

  3. Write a Python script to calculate the average of three numbers entered by the user.

  4. Write a Python script to calculate the volume of a cuboid.

  5. Write a Python script to take two numbers from the user (say x and y), calculate x^y and display the result.

P =100
n =10
r = 10

print((P*n*r)/100)
100.0

Assignment 10: Operators

  1. Write a Python script to remove the last digit from a given number.

    Example: Input: 2534 Output: 253

  2. Write a Python script to get the last digit from a given number.

    Example: Input: 2089 Output: 9

  3. Write a Python script to swap the data of two variables.

  4. Write a Python script which takes a three-digit number from the user and displays only its first digit.

  5. Write a Python script which takes a three-digit number from the user and displays only its middle digit.

x = 109
x//10
10
x/10
10.9
x%10
9
x = 10
y = 12
x,y = y,x
x,y
(12, 10)
x = int(input('give a 3 didgit number'))
give a 3 didgit number987
x//100
9
(x//10)%10
8

Assignment 11: More on Operators

  1. Write a Python script to print True if the string ‘my’ is a member of a string entered by the user.

  2. Write a Python script to input two strings from the user and display whether the two variables refer to the same object or not. Print True or False.

  3. What will be the output of the Python expression:

    5 > 10 < 5

  4. What will be the output of the Python expression:

    “Red” and “White”

  5. What will be the output of the Python expression:

    True or False

x = "my friend"
print("my" in x)
True
"my" == "my"
True
5>10<5
False
"Red" and "black"
'black'
True or False
True

Assignment 12: Decision Control

  1. Write a Python script to check whether a given number is positive or non-positive.

  2. Write a Python script to check whether a given number is divisible by 5 or not.

  3. Write a Python script to check whether a given number is even or odd.

  4. Write a Python script to print the greater of two numbers. Print the number only once even if the numbers are the same.

  5. Write a Python script to print two given words in dictionary order.

if condition:

\(~~\)body

x = -10
if x>0:
  print("positive")
if x<0:
  print("negative")

if-else

if condition1:

\(~~\)body

else:

\(~~\)body

if x>0:
  print("positive")
else:
  print("negative")
negative
x = 10

if x%5 == 0:
  print("divisible by 5")
else:
  print("not divisible by 5")
divisible by 5
a = 10
b = 5
if a>b:
  print(a)
else:
  print(b)
10
"apple">"aaple"
True
print(ord('m'))
109
print(ord('o'))
111

Assignment 13: More on Decision Control

  1. Write a Python script to check whether a given number is a three-digit number or not.

  2. Write a Python script to check whether a given number is positive, negative or zero.

  3. Write a Python script to check whether a given quadratic equation has real and distinct roots, real and equal roots, or imaginary roots.

  4. Write a Python script to check whether a given year is a leap year or not.

  5. Write a Python script to print the greater among three numbers. Print the number only once even if some numbers are the same.

Assignment 15: While Loop

  1. Write a Python script to print MySirG 5 times on the screen.

  2. Write a Python script to print the first 10 natural numbers.

  3. Write a Python script to print the first 10 natural numbers in reverse order.

  4. Write a Python script to print the first 10 odd natural numbers.

  5. Write a Python script to print the first 10 odd natural numbers in reverse order.