In Python, variables are like containers that hold data values. When you create a variable, you are essentially allocating space in the computer's memory to store that data.
Variables:
Imagine a variable as a labeled box. You give the box a name (like "x" or "name"), and it can hold different things, like numbers, words, or even lists of things.
Variables are created by giving them a name and assigning them a value using the assignment operator =. For example: x = 5 or name = "John".
Variable names can consist of letters (both uppercase and lowercase), digits, and underscores, but they cannot start with a digit.
Python is dynamically typed, meaning you don't need to explicitly declare the type of a variable. Python determines the data type based on the value assigned to it.
Data Types:
Each box (variable) can hold different types of things, like numbers, words, or collections of items.
For example, you might have a box called "age" that holds a number like 25, or a box called "name" that holds a word like "John".
Numeric Types: Integers (int): Whole numbers, e.g., 5, -10, 1000.
Floating-point numbers (float): Numbers with a decimal point or in exponential form, e.g., 3.14, -0.001, 2.5e2.
Boolean Type (bool): Represents truth values True or False.
String Type (str): A sequence of characters enclosed within single (') or double (") quotes, e.g., 'hello', "Python".
Lists (list): Ordered and mutable collections of items, enclosed within square brackets [], e.g., [1, 2, 3], ['apple', 'banana', 'orange'].
Tuples (tuple): Ordered and immutable collections of items, enclosed within parentheses (), e.g., (1, 2, 3), ('apple', 'banana', 'orange').
Dictionaries (dict): Unordered collections of key-value pairs, enclosed within curly braces {}, e.g., {'name': 'John', 'age': 30}.
Sets (set): Unordered collections of unique elements, enclosed within curly braces {}, e.g., {1, 2, 3}.
Comments
Post a Comment