0% found this document useful (0 votes)
0 views8 pages

X State - Basics of Python

Uploaded by

nanditha629
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)
0 views8 pages

X State - Basics of Python

Uploaded by

nanditha629
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/ 8

Chapter 2 – More on Python

D. Name the following:


1. Two types of arithmetic operators used : unary and binary
2. Three types of logical operators: &&, || and !
3. Two operators used in strings: + and *
4. The operator used to find the remainder when one value is divided by the
other: %
5. The data type used to represent logical values: Boolean

E. Answer the following questions:


1. What are data types? Explain the main data types used in python.
Ans: Data types in Python define the type of data a variable can hold. Each
value in Python has a specific data type.
Main Data Types in Python:
1. Integer (int)
Whole numbers without decimals.
Example: 5, -10, 1000
2. Float (float)
Numbers with decimals.
Example: 3.14, -0.5, 100.0
3. String (str)
Text or characters, written inside quotes.
Example: "Hello", 'Python123'
4. Boolean (bool)
Only two values: True or False.
Used in conditions and comparisons.
5. List
A collection of items, written in square brackets.
Example: [1, 2, 3], ["apple", "banana"]
6. Tuple
Like a list, but cannot be changed.
Example: (1, 2, 3)
7. Dictionary (dict)
Stores data in key-value pairs.
Example: {"name": "John", "age": 15}
These data types help us store and work with different kinds of information in Python
programs.

2. What do you understand by the term ‘operator’?


Ans: An operator is a symbol or function that performs an operation on one or
more operands (variables or values). Operators are used to perform operations
like addition, comparison, assignment, and more.
Eg:
a=5+3
Here, ‘+’ is the operator
3. What is the difference between ‘/’ and ‘//’ operator?
/ operator // operator
Name: Division Name: Floor Division
Performs true (floating-point) Performs integer (floor) division.
division. Always returns a float Returns the largest integer ≤ result.

4. What is the use of ‘*’ operator in a string manipulation?


Ans: In Python, the * operator repeats a string multiple times.
text = "Hi! "
result = text * 3
print(result)

O/P: Hi! Hi! Hi!

5. Define associativity of operators in python.


Ans: Associativity defines the order in which operators of the same precedence
are evaluated in an expression.
When an expression has multiple operators of the same precedence level,
associativity tells us whether to evaluate from left to right or right to left.
Most operators like +, -, *, / are left-associative (evaluate left to right).
The exponentiation operator ** is right-associative (evaluate right to left).

6. What is the significance of the Logical operators in python?


Ans: Logical operators are used to combine multiple conditional statements or
to invert conditions in Python. They help in making decisions in your program
by evaluating complex logical expressions and returning a Boolean result (True
or False).
Operator Decription Example
and Returns True if both (x>0) and (x<10)
operands are True
or Returns True if at least (x>0) or (x<10)
one operand is True
not Returns the opposite of not(x == 5)
the operand's Boolean
value

7. Describe the use of input() function with example.


Ans: The input() function is used to take input from the user through the
keyboard. Whatever the user types is always received as a string.
For example:
name = input("Enter your name: ")
print("Hello, " + name)

O/P: Enter your name: Anu


Hello, Anu
8. What are escape sequences? List any two

Ans: Escape sequences are special characters used in strings to represent things
that can't be typed easily, like new lines or tabs.
They start with a backslash \ followed by a character.
Two Common Escape Sequences:
1. \n – New line - It moves the text to the next line.
print("Hello\nWorld")

O/P: Hello
World

2. \t – Tab space - It adds a horizontal tab (like pressing the TAB key).

print("Name:\tAnanya")

O/P: Name: Ananya

9. What is the use of print() function?


The print() function in Python is used to display output on the screen.
It shows text, numbers, or results of calculations so the user can see them.
print("Hello, World!")

O/P: Hello, World!

10. Distinguish between relational and logical operators.

Logical Operators
Relational Operators
Used to combine two or more
Used to compare two values conditions

Returns True or False based on logic


Returns True or False based on
comparison
and, or, not are the logical operators
==, !=, >, <, >=, <= are the relational
operators
Used for making decisions using
Used for checking the relationship
multiple conditions
between values
Activity Zone:

A. Evaluate the following expressions:

a) 2 * (2*3) * (3**2) * (-1**3) + 6/2 - 3*2


= 2*6*9*(-1) + 3.0 - 6
= 12 * 9 * -1 + 3.0 - 6
= -108 + 3.0 - 6
= -111.0

b) (2*3) + 2(3**2) + (-1)


= 6 + 2(9) + (-1)
= 6 + 18 + (-1)
= 23

c) (xy + 2y2 + z)/(x+y+z) for x=3, y=2 and z=1


= (6+8+1)/6
= 15/6
= 2.5

B. Find the output of the following programs:


1)
num1 = 8
num2 = num1 ** 0.5
print(num2)

O/p: 2.8284271247461903

2)
num = int(input("Enter a number"))
print(num)

O/P: Enter a number: 5


5

3)
num = float(input("Enter a number"))
print(num)

O/P: Enter a number: 3.14


3.14
4)
c = float(input("Enter temperature in Celsius: "))
f = (9 / 5) * c + 32
print("The temperature is:", f)

O/P: Enter temperature in Celsius: 25


The temperature is: 77.0

5) name=”abc”
print(name)
name=”xyz”
print(name)

O/P: abc
Xyz

6) x,y,z=10,4.5,”Welcome”
print(x)
print(y)
print(z)

O/P: 10
4.5
Welcome

Lab Activity:
a. Print your name 10 times using the string Replication operator
print(("Class X State\n" * 10))
b. Program to add 2 numbers:
# Program to add two numbers
# Input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Add the numbers
sum = num1 + num2
# Display the result
print("The sum is:", sum)

c. Program to subtract 2 numbers:


# Input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Subtract the numbers
diff = num1 - num2
# Display the result
print("The difference is:", diff)

d. Program to find the remainder:


# Input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Find the remainder
rem = num1 % num2
# Display the result
print("The remainder is:", rem)

e. Program to find the quotient of 2 numbers:


# Input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Find the quotient
quo = num1 / num2
# Display the result
print("The quotient is:", quo)

f. Program to find the quotient of 2 numbers when integer division is


performed:
# Input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Find the quotient
quo = num1 // num2
# Display the result
print("The quotient is:", quo)

g. Program to find the product of 2 numbers:


# Input from the user
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
# Find the product
prod = num1 * num2
print("The product is:", prod)

h. Input the name, age and basic salary of an employee. Calculate the total
salary of an employee by adding 10% da and 10% HRA to the basic salary

# Program to calculate total salary of an employee


# Input from user
name = input("Enter employee name: ")
age = int(input("Enter employee age: "))
basic_salary = float(input("Enter basic salary: "))

# Calculate DA and HRA


da = 0.10 * basic_salary
hra = 0.10 * basic_salary
# Calculate total salary
total_salary = basic_salary + da + hra

# Display the result


print("Employee Details")
print("Name:", name)
print("Age:", age)
print("Basic Salary:", basic_salary)
print("DA (10%):", da)
print("HRA (10%):", hra)
print("Total Salary:", total_salary)

i) Write a program to calculate the area of a triangle by reading the base


and height from the user.

# Read input from the user


base = float(input("Enter the base of the triangle: "))
height = float(input("Enter the height of the triangle: "))

# Calculate area
area = 0.5 * base * height

# Display the result


print("The area of the triangle is:", area)

j) Write a program to calculate the area of a square by reading the length


of a side

side = float(input("Enter the length of the side of the square: "))

# Calculate the area


area = side * side
# Display the result
print("The area of the square is:", area)
k) Write a python program to input roll_no, name and marks in 5 subjects.
Calculate the total and the average percentage.

# Input roll number and name


roll_no = input("Enter roll number: ")
name = input("Enter name: ")

# Input marks for 5 subjects


marks = []
for i in range(1, 6):
mark = float(input(f"Enter marks for subject {i}: "))
marks.append(mark)

# Calculate total and average percentage


total = sum(marks)
average = total / 5

# Display results
print("\nStudent Details:")
print("Roll Number:", roll_no)
print("Name:", name)
print("Total Marks:", total)
print("Average Percentage:", average, "%")

You might also like