Variables and Data Types of Python by Black Box AI | Ballu Python 835430
BLACKBOXAI
In Python, variables are used to store data values, and data types define the type of data that can be stored in a variable. Python is dynamically typed, meaning you don't need to declare the type of a variable when you create it. Here’s a detailed overview of variables and data types in Python:
Variables
Definition: A variable is a name that refers to a value. You can think of it as a container for storing data.
Naming Rules:
- Must start with a letter (a-z, A-Z) or an underscore (_).
- Can be followed by letters, numbers (0-9), or underscores.
- Case-sensitive (e.g.,
myVarandmyvarare different variables). - Cannot be a keyword (reserved word in Python).
Example:
python1age = 25 2name = "Alice" 3is_student = True
Data Types
Python has several built-in data types, which can be categorized into the following main types:
1. Numeric Types
int: Represents integer values (whole numbers).
- Example:python
1x = 10 2y = -5
- Example:
float: Represents floating-point numbers (decimal values).
- Example:python
1pi = 3.14 2temperature = -10.5
- Example:
complex: Represents complex numbers, which have a real and an imaginary part.
- Example:python
1z = 2 + 3j # 2 is the real part, 3 is the imaginary part
- Example:
2. Sequence Types
str: Represents strings, which are sequences of characters.
- Example:python
1greeting = "Hello, World!"
- Example:
list: Represents an ordered, mutable collection of items. Lists can contain mixed data types.
- Example:python
1fruits = ["apple", "banana", "cherry"] 2numbers = [1, 2, 3, 4.5, "five"]
- Example:
tuple: Represents an ordered, immutable collection of items. Like lists, tuples can also contain mixed data types.
- Example:python
1coordinates = (10.0, 20.0)
- Example:
3. Mapping Type
- dict: Represents a collection of key-value pairs. Dictionaries are unordered and mutable.
- Example:python
1student = { 2 "name": "Alice", 3 "age": 25, 4 "is_student": True 5}
- Example:
4. Set Types
set: Represents an unordered collection of unique items. Sets are mutable.
- Example:python
1unique_numbers = {1, 2, 3, 4, 5}
- Example:
frozenset: Represents an immutable version of a set.
- Example:python
1immutable_set = frozenset([1, 2, 3, 4, 5])
- Example:
5. Boolean Type
- bool: Represents Boolean values, which can be either
TrueorFalse.- Example:python
1is_active = True 2is_logged_in = False
- Example:
Type Checking and Conversion
Checking Data Type: You can check the type of a variable using the
type()function.python1print(type(age)) # Output: <class 'int'> 2print(type(name)) # Output: <class 'str'>Type Conversion: You can convert between different data types using built-in functions like
int(),float(),str(), etc.python1num_str = "123" 2num_int = int(num_str) # Converts string to integer 3print(num_int) # Output: 123
Summary
In summary, Python provides a variety of data types to handle different kinds of data, and variables are used to store these data types. Understanding how to use variables and data types is fundamental to programming in Python.
Comments
Post a Comment