0% found this document useful (0 votes)
3 views2 pages

python_data_types_QA

The document outlines basic data types in Python, including int, float, str, bool, list, tuple, set, and dict. It provides examples of variable type checking, type conversion, arithmetic operations, string manipulation, list and dictionary operations, and checking for even or odd numbers. Additionally, it demonstrates the use of sets for unique values and the immutability of tuples.

Uploaded by

Rajan Kumar
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)
3 views2 pages

python_data_types_QA

The document outlines basic data types in Python, including int, float, str, bool, list, tuple, set, and dict. It provides examples of variable type checking, type conversion, arithmetic operations, string manipulation, list and dictionary operations, and checking for even or odd numbers. Additionally, it demonstrates the use of sets for unique values and the immutability of tuples.

Uploaded by

Rajan Kumar
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/ 2

1. What are the basic data types in Python?

The most common ones are: - int → integers (10, -5) - float → decimal numbers (3.14,
-0.7) - str → text ("Hello") - bool → True/False - list → ordered, mutable collection [1,2,3] -
tuple → ordered, immutable collection (1,2,3) - set → unordered, unique elements {1,2,3} -
dict → key-value pairs {"name":"Rajan", "age":25}

2. Program to check the type of a variable

x = 25 y = 3.14 z = "Python" a = True print(type(x)) # int print(type(y)) # float print(type(z))


# str print(type(a)) # bool

3. Convert one data type to another

num = 100 print(float(num)) # int → float print(str(num)) # int → str print(bool(num)) # int →
bool (non-zero → True)

4. Add two numbers (int and float)

a = 10 # int b = 5.5 # float result = a + b print("Sum:", result) # Output: 15.5 print("Type:",


type(result)) # float

5. String operations

s = "Python" print(s.upper()) # PYTHON print(s.lower()) # python print(s[0]) # P print(s[-1])


# n print(s[2:5]) # tho

6. List operations

fruits = ["apple", "banana", "cherry"] fruits.append("mango") fruits.remove("banana")


print(fruits) # ['apple', 'cherry', 'mango']

7. Dictionary example

student = {"name": "Rajan", "age": 22, "course": "CS"} print(student["name"]) # Rajan


student["age"] = 23 # update student["marks"] = 90 # add new print(student)

8. Program to check if a number is even or odd

num = int(input("Enter a number: ")) if num % 2 == 0: print("Even") else: print("Odd")

9. Program using Set (unique values)

nums = [1, 2, 2, 3, 4, 4, 5] unique_nums = set(nums) print(unique_nums) # {1, 2, 3, 4, 5}


10. Tuple Example

t = (10, 20, 30) print(t[0]) # 10 print(len(t)) # 3 # t[1] = 40 # ■ Error (immutable)

You might also like