Variables in Python (Part 3)

Posted by

1.Variables in Python

  • Variables in Python
  • Variable names in Python
  • Assign multiple values to variables

Python Variables

  • Variables are like containers for storing data values.
  • Variable names are case-sensitive.
  • Python has no command for declaring a variable. Variable will get created the moment you assign a value to it.
  • Variables can change type after they have been set.
  • We can get the type of variable using the type() function.
  • No need to declare what type of variable it is

  • Variables act like containers: In Python, variables are used to store information like numbers, text, or more complex data structures. You can think of them as named boxes that hold data you can use throughout your program.
  • Case sensitivity matters: Python is case-sensitive, so name and Name are considered different variables. This means that you can’t use the same name with different capitalizations to store different data.
  • Implicit variable creation: Unlike some other programming languages, Python doesn’t require you to declare a variable before using it. You create a variable simply by assigning a value to it. For instance, if you write age = 30, you’ve created a variable named age and assigned the value 30 to it.
  • Flexibility with data types: Variables in Python are flexible and can change their data type. If you assign a number to a variable, and later assign text to the same variable, the variable’s data type will automatically switch to text.
  • Checking variable type: The type() function allows you to determine the data type of a variable. This can be useful when working with different types of data in your code.

To Checking variable type

Variables can change type after they have been set.

if we assigned string value in double quote like below

Variable names

  • Must start with a letter or underscore: Variable names in Python must begin with a letter (uppercase or lowercase) or an underscore character (_).
  • Numbers can follow: After the first letter or underscore, variable names can contain letters, numbers, and underscores.
  • Examples of valid names: Here are some examples of valid variable names: age, total_cost, _counter, user_input.
  • Examples of invalid names: Names that violate the rules above or are reserved keywords in Python are invalid. Here are some examples of invalid variable names: 2ndplace (starts with a number), $revenue (contains a special character), def (a reserved keyword in Python).
  • Case sensitivity matters: Remember that Python is case-sensitive. So, age and Age are considered different variables.

Assign multiple values to variables

  • There are two main ways to assign multiple values to variables in Python:
  1. Assigning to multiple variables: You can assign a series of values to multiple variables separated by commas in a single line. The order of the values matters. The first value is assigned to the first variable, the second value is assigned to the second variable, and so on.

For example:

x, y, z = "apple", "banana", "cherry"
print(x)  # Output: apple
print(y)  # Output: banana
print(z)  # Output: cherry

  1. one value to multiple Variables:

For example:

x=y=z = "apple"
print(x)  # Output: apple
print(y)  # Output: banana
print(z)  # Output: cherry

2. Global Variables & Local Variables in Python


  • Global Variables
  • Local Variables
  • global Keyword

Local Variables

  • Local variables are created inside a function.
  • Local variables can only be used inside the function.

Function is piece of code which we save and keep it with some name and call it with name whenever required

To create function use def keyword

def myfun():
    x = "Ravi"


Global Variables

  • Global variables are variables that are created outside of a function.
  • They are accessible from anywhere in the program, including inside functions.

having both local and global value same

Global Keyword

  1. Declare a global variable outside the function: The text says that a global variable must first be declared outside of any function definitions. This creates a variable that is accessible throughout your entire program.
  2. Use the global keyword inside the function: Within the function you want to modify the global variable, you must declare it as global using the global keyword. This tells Python that you’re working with the same variable you declared outside the function, not creating a new local variable.
  3. Modify the global variable: Once you’ve declared the variable as global inside the function, you can then modify its value.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x