Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Variables in Python

Variables in Python

Variables in Python

In Python, variables are used to store values. These values can be of different data types like integers, floats, strings, and more. Python is dynamically typed, which means you do not need to explicitly declare the type of a variable before using it. Python automatically infers the type based on the assigned value.


1. Declaring Variables

In Python, you can create a variable by simply assigning a value to it using the = (assignment) operator.

Syntax:

variable_name = value

Example:

name = "Alice"  # String variableage = 25        # Integer variableheight = 5.6    # Float variable
  • In this example, name is a variable storing a string, age stores an integer, and height stores a float.


2. Variable Naming Rules

When naming variables in Python, you must follow these rules:

  • A variable name must begin with a letter (a-z, A-Z) or an underscore (_).

  • The rest of the variable name can contain letters, digits (0-9), and underscores.

  • Variable names are case-sensitive, so age and Age would be treated as different variables.

  • Variable names cannot be Python keywords (e.g., if, else, def, class).

Examples of valid variable names:

age = 30first_name = "John"_height = 1.75

Examples of invalid variable names:

2nd_name = "Alice"  # Invalid: starts with a digitclass = "Math"      # Invalid: 'class' is a reserved keyword

3. Assigning Multiple Variables

Python allows you to assign values to multiple variables in a single line.

Example:

x, y, z = 10, 20, 30print(x)  # Output: 10print(y)  # Output: 20print(z)  # Output: 30
  • Here, the values 10, 20, and 30 are assigned to variables x, y, and z, respectively.


4. Reassigning Variables

Variables in Python are mutable. You can reassign a new value to a variable at any time.

Example:

age = 25print(age)  # Output: 25age = 30print(age)  # Output: 30
  • In this example, the value of age is reassigned from 25 to 30.


5. Type of a Variable

You can check the type of a variable using the type() function.

Example:

a = 10b = "Hello"c = 3.14print(type(a))  # Output: <class 'int'>print(type(b))  # Output: <class 'str'>print(type(c))  # Output: <class 'float'>
  • type() returns the type of the variable, showing whether it's an integer (int), string (str), float (float), etc.


6. Constants in Python

Although Python does not have a built-in constant type, by convention, variables that are not meant to be modified are written in uppercase letters, which is used to represent constants.

Example:

PI = 3.14159  # Constant
  • This is a convention, not a rule enforced by Python. It’s common to use uppercase variable names for values that shouldn't change.


7. Global and Local Variables

  • Local Variables: Defined within a function and can only be accessed inside that function.

  • Global Variables: Defined outside any function, accessible throughout the program.

Example of Local Variable:

def my_function():    local_var = 100    print(local_var)my_function()  # Output: 100
  • local_var is only accessible inside my_function().

Example of Global Variable:

global_var = 50def my_function():    print(global_var)my_function()  # Output: 50
  • global_var is accessible both inside and outside of the function.

To modify a global variable inside a function, you must use the global keyword.

Example of Modifying a Global Variable:

x = 10def modify_global():    global x    x = 20modify_global()print(x)  # Output: 20

8. Multiple Assignment with a Single Value

Python also allows you to assign the same value to multiple variables in a single line.

Example:

a = b = c = 50print(a, b, c)  # Output: 50 50 50
  • Here, a, b, and c all share the same value 50.


9. The del Keyword

The del keyword can be used to delete a variable.

Example:

x = 10del x# print(x)  # This will raise an error because x is deleted
  • After del x, the variable x no longer exists.


10. Conclusion

  • Variables in Python are used to store data.

  • Python is dynamically typed, meaning you don't need to declare the variable type explicitly.

  • Python variables are easy to work with and follow simple naming conventions.

  • You can easily change the value of a variable during the program execution.

By understanding how to create and manage variables, you can store and manipulate data efficiently in your Python programs.

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql