0% found this document useful (0 votes)
14 views6 pages

Chapter 2 - Data Types & Operators

The document provides an overview of Python's data types, keywords, operators, and type conversions, along with examples and practice questions. It explains how to use built-in functions for data type checking and conversion, and includes coding assignments for practical application. The content is structured into sections covering theory and coding exercises to reinforce learning.

Uploaded by

numansidhu314
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)
14 views6 pages

Chapter 2 - Data Types & Operators

The document provides an overview of Python's data types, keywords, operators, and type conversions, along with examples and practice questions. It explains how to use built-in functions for data type checking and conversion, and includes coding assignments for practical application. The content is structured into sections covering theory and coding exercises to reinforce learning.

Uploaded by

numansidhu314
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

1️⃣ Data Types in Python

Python has several built-in data types. They define the type of value a variable
holds.

Type Example Description

int x = 10 Whole numbers (positive or


negative)

float y = 3.14 Numbers with decimals

str name = "Saumya" Sequence of characters

bool flag = True Logical values: True or False

📘 Note: Use the type() function to check a variable’s data type.


x = 10
print(type(x))

# <class 'int'>

Practice Question:

Write a Python program that takes your age as input and prints:

●​ The value entered​

●​ Its data type​

2️⃣ Keywords in Python

Keywords are reserved words that have special meaning in Python and cannot
be used as variable names.

Examples of common keywords:


and ​ as ​ assert ​ break ​ class
continue ​ def ​​ del ​ elif ​ else
except ​ False ​ finally ​ for ​ from
global ​ if ​ ​ import ​ in ​ is
lambda ​ None ​ nonlocal ​ not ​ or
pass ​ raise ​​ return ​ True ​ try
while ​ with ​​ yield

🧠 Tip: Use help("keywords") in Python shell to list all current keywords.


Practice Question:​
Try to create a variable named for in Python. What error do you get and why?

3️⃣ Print Sum Program

Example program to input two numbers and print their sum:

# Program to find sum of two numbers


a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
sum = a + b
print("The sum is:", sum)

🧩 Explanation:
●​ input() takes user input as a string​

●​ int() converts it to an integer​

●​ print() displays the result​

Practice Question:
Modify this program to find the average of two numbers instead of the sum.

4️⃣ Type Conversions


Type conversion means changing one data type to another.

a. Implicit Conversion (Automatic)

Python automatically converts smaller data types to larger ones to prevent data
loss.

x=5 # int
y = 2.5 # float
z = x + y # Python converts int → float
print(z) # 7.5

b. Explicit Conversion (Manual)

Manually convert data types using built-in functions:

x = "10"
y = int(x) # str → int
print(y + 5) # Output: 15

🧠 Common Functions: int(), float(), str(), bool()


Practice Question:

Take a number as input, convert it to a float, and print both the original and
converted values with their data types.

5️⃣ Operators in Python

Operators perform operations on variables and values.

Type Example Description

Arithmetic + - * / % ** Math operations (** for power)

Comparison == != > < >= <= Compare values, returns True or False

Logical and or not Combine conditions


Assignment = += -= *= /= Assign or modify values

🧮 Example:
x=5
y=3
print(x + y) # 8
print(x > y) # True
print(x > 0 and y < 20) # True (both conditions are True)

Practice Question:​
Write a program that takes two numbers and prints:

●​ Their sum, difference, and product​

●​ Whether the first number is greater than the second​

✅ Summary
●​ Data types define what kind of value a variable can hold.​

●​ Keywords are reserved words with predefined meanings.​

●​ Print sum program helps understand input, conversion, and output.​

●​ Type conversion can be implicit (auto) or explicit (manual).​

●​ Operators perform actions like math, comparison, logic, etc.

Python Chapter 2 Assignment

📘 Section A: Theory Questions


(Answer in short)
1.​ What are data types in Python? List any 4 with examples.​

2.​ What is the difference between implicit and explicit type conversion? Give
one example of each.​

3.​ What are operators in Python? Explain any three types with examples.​

💻 Section B: Coding Questions


1️⃣ Smart Temperature Converter

Take input in Celsius and print its equivalent in Fahrenheit and Kelvin.​
(Use explicit type conversion and arithmetic operators.)​
Formula:

●​ Fahrenheit = (C × 9/5) + 32​

●​ Kelvin = C + 273.15​

Example:

Enter temperature in Celsius: 25


Output:
Fahrenheit: 77.0
Kelvin: 298.15

2️⃣ Bill Split Calculator

Write a program that takes total bill amount and number of friends as input.​
Calculate how much each person will pay.​
Also print the data type of each variable used.

(Hint: use float() and division operator)

Total bill: 1000


Friends: 4
Each will pay: 250.0
🧠 Section C: Application / Output-Based
1.​ Predict the output:
x = 5
y = 2.0
print(x // y)
print(x ** y)

2.​

Identify and correct the error:​



if = 10
print(if)

You might also like