0% found this document useful (0 votes)
2 views

Day1- Data Types in Python

The document outlines various data types in Python, including integer, float, string, boolean, list, tuple, dictionary, and set. It explains how to declare variables dynamically and perform type conversion using built-in functions. Additionally, it provides examples of variable declaration, type conversion, and user input handling in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Day1- Data Types in Python

The document outlines various data types in Python, including integer, float, string, boolean, list, tuple, dictionary, and set. It explains how to declare variables dynamically and perform type conversion using built-in functions. Additionally, it provides examples of variable declaration, type conversion, and user input handling in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Data Types in Python:

Python supports various data types, including:


1. Integer: Represents whole numbers without any decimal point.
2. Float: Represents real numbers with decimal points.
3. String: Represents a sequence of characters enclosed within single
quotes (' ') or double quotes (" ").
4. Boolean: Represents either True or False.
5. List: Represents an ordered collection of items enclosed within
square brackets [ ].
6. Tuple: Similar to lists but immutable (cannot be modified after
creation), enclosed within parentheses ( ).
7. Dictionary: Represents a collection of key-value pairs enclosed
within curly braces { }.
8. Set: Represents an unordered collection of unique items enclosed
within curly braces { }.

Declaring Variables:

In Python, variables are declared simply by assigning a value to a


name. Python is dynamically typed, meaning you don't need to specify
the data type explicitly; Python infers it automatically based on the
value assigned.

Type Conversion Operations:


Python provides built-in functions to convert one data type to another.
For example:
1. int(): Converts a value to an integer.
2. float(): Converts a value to a floating-point number.
3. str(): Converts a value to a string.
4. bool(): Converts a value to a boolean.
5. list(): Converts a sequence to a list.
6. tuple(): Converts a sequence to a tuple.
7. dict(): Converts a sequence of key-value pairs to a dictionary.
8. set(): Converts a sequence to a set.
Example 1: Variable Declaration and Type Conversion
# Declare variables of different data types
x = 10 # Integer
y = 3.14 # Float
name = "John Doe" # String
is_student = True # Boolean

# Print the data types of variables


print("Data types:")
print("x is of type:", type(x))
print("y is of type:", type(y))
print("name is of type:", type(name))
print("is_student is of type:", type(is_student))

# Perform type conversion operations


x_str = str(x) # Convert integer to string
y_int = int(y) # Convert float to integer
is_student_str = str(is_student) # Convert boolean to string

# Print the converted variables


print("\nConverted variables:")
print("x_str:", x_str)
print("y_int:", y_int)
print("is_student_str:", is_student_str)

Explanation:
9. We declare variables x, y, name, and is_student of different data
types: integer, float, string, and boolean, respectively.
10. We print the data types of these variables using the type()
function.
11. We perform type conversion operations using functions like str(),
int(), and bool() to convert variables to different data types.
12. Finally, we print the converted variables.

Output:

Data types:
x is of type: <class 'int'>
y is of type: <class 'float'>
name is of type: <class 'str'>
is_student is of type: <class 'bool'>

Converted variables:
x_str: 10
y_int: 3
is_student_str: True

Example 2: Type Conversion from User Input

# Get user input for a number


num_str = input("Enter a number: ")

# Convert the input to an integer


num_int = int(num_str)

# Perform arithmetic operation


result = num_int * 2

# Print the result


print("Twice the number is:", result)

Explanation:
1. We use the input() function to get user input, which is initially
stored as a string.
2. We convert the input string to an integer using the int() function.
3. We perform an arithmetic operation (multiplication by 2) on the
converted integer.
4. Finally, we print the result.

Output (Sample):

Enter a number: 5
Twice the number is: 10

You might also like