1. Variables:
Variables are used to store data values. In Python, variables are created when you assign a value to them. Unlike some other programming languages, you don't need to declare the variable type explicitly; Python infers the data type based on the assigned value.
Example:
x = 10 # Integer variable y = 3.14 # Float variable name = "John" # String variable is_student = True # Boolean variable
In this example:
xis an integer variable storing the value10.yis a float variable storing the value3.14.nameis a string variable storing the value"John".is_studentis a boolean variable storing the valueTrue.
2. Data Types:
Python supports several built-in data types, each designed to represent different kinds of data. Here are some common data types in Python:
a. Numeric Types:
- int: Represents integer values, e.g.,
10,-5,1000. - float: Represents floating-point (decimal) values, e.g.,
3.14,2.5,1.0.
b. String Type:
- str: Represents sequences of characters enclosed within single (
') or double (") quotes, e.g.,"Hello",'Python',"123".
c. Boolean Type:
- bool: Represents Boolean values
TrueorFalse.
d. Sequence Types:
- list: Represents ordered collections of items, which can be of different data types and mutable (modifiable), e.g.,
[1, 2, 3],['a', 'b', 'c']. - tuple: Represents ordered collections of items, similar to lists but immutable (cannot be modified), e.g.,
(1, 2, 3),('a', 'b', 'c'). - range: Represents sequences of numbers, commonly used for looping, e.g.,
range(5)generates numbers from0to4.
e. Mapping Type:
- dict: Represents collections of key-value pairs, where each key is associated with a value, e.g.,
{'name': 'John', 'age': 30}.
f. Set Types:
- set: Represents unordered collections of unique items, e.g.,
{1, 2, 3}. - frozenset: Similar to sets but immutable.
g. None Type:
- NoneType: Represents the absence of a value, commonly used to indicate null or undefined values, e.g.,
None.
Example:
# Numeric types x = 10 # int y = 3.14 # float # String type name = "John" # str # Boolean type is_student = True # bool # Sequence types my_list = [1, 2, 3] # list my_tuple = (4, 5, 6) # tuple my_range = range(5) # range # Mapping type my_dict = {'name': 'John', 'age': 30} # dict # Set types my_set = {1, 2, 3} # set my_frozenset = frozenset({4, 5, 6}) # frozenset # None type value = None # NoneType
0 Comments