Python Variables
and Data Types
int: Integer type (whole
numbers).
For Example :
age
= 25
float: Floating-point type
(decimal numbers).
For
Example :
height
= 1.75
complex: Complex numbers.
For
Example :
complex_num
= 3 + 4j
Text Type:
str: String type (sequence of
characters).
For Example :
name
= "John"
Strings can
be enclosed in single (')
or double (")
quotes.
Boolean Type:
bool: Boolean type (True or
False).
For Example : is_adult
= True
Sequence Types:
list: Ordered, mutable
sequence.
For Example :
fruits
= ["apple", "banana", "orange"]
tuple: Ordered, immutable
sequence.
For Example :
coordinates = (3, 4)
str: String
is also a sequence of characters.
Mapping Type:
dict: Dictionary, an unordered
collection of key-value pairs.
For Example :
person
= {"name": "Alice", "age": 30}
Set Types:
set: Unordered collection of
unique elements.
For Example :
unique_numbers
= {1, 2, 3, 4, 5}
None Type:
None: Represents the absence of a value or a
null value.
For Example :
no_value
= None
Type Conversion:
You can convert between different data types using
built-in functions like int(), float(), str(),
etc.
For Example :
num_str
= "25"
num_int
= int(num_str)
Variable Assignment:
Variables are assigned using the =
operator.
For Example :
x
= 10
You can assign values of different types to the
same variable.
For Example :
x
= "Hello" x = 42
Dynamic Typing:
Python is dynamically typed, so you don't need to
declare the type of a variable explicitly.
For Example :
dynamic_variable
= "Hello, Python!"
These are the basic data types in Python.
Understanding how to use and manipulate these data types is fundamental to
writing effective Python code.
Comments
Post a Comment