Coding Schools


 
Python | C Sharp | Azure AI | HTML | JavaScript | CSS | SQL Server
Python - Variable declarations
Conditional Statements in Python
Loops in Python
Python Functions
Python Classes
OOPS in Python
Python - Hello World Application
Python - import statements
Python - Web API

Python - Variable declarations

In Python, declaring a variable is simple and straightforward. You just need to assign a value to a name using the = operator. Here's a basic example:

python
# Integer variable
age = 25

# Float variable
pi = 3.14159

# String variable
name = "Alice"

# Boolean variable
is_student = True

# List variable
fruits = ["apple", "banana", "cherry"]

# Dictionary variable
person = {"name": "Alice", "age": 25}

# Tuple variable
coordinates = (100, 200)

Key Points:

  1. Dynamic Typing: Python is dynamically typed, meaning the type of a variable is determined at runtime and you don't need to declare the type of the variable explicitly.

    python
    variable = 10     # Now variable is an integer
    variable = "text" # Now variable is a string
    
  2. Case Sensitivity: Variable names are case-sensitive, so age and Age would be considered different variables.

    python
    age = 25
    Age = 30
    
  3. Naming Conventions: It's a good practice to use meaningful variable names and follow naming conventions such as using lowercase letters and underscores (_) for readability.

    python
    student_name = "Bob"
    student_age = 20
    
  4. Reserved Keywords: Avoid using Python's reserved keywords (like def, class, if, else, etc.) as variable names.

    python
    # This would raise an error
    def = 42  
    
  5. Reassignment: Variables in Python can be reassigned to different types of data. The new assignment will completely overwrite the previous value and type.

    python
    my_var = "hello"
    my_var = 42
    

Feel free to let me know if you have any specific scenarios or questions about variable declaration!




All rights reserved | Privacy Policy | Sitemap