No Widget Added

Please add some widget in Offcanvs Sidebar

Shopping cart

shape
shape

Python Variables

Python variable

A name given to a memory location is called Variable. And value holding python variable is known as Identifier. Python itself is smart enough language to determine the type of a variable, we do not need to specify its type in Python.

Key points for variable

1 Variable names must begin with a letter or an underscore
2 They can be a group of both letters and digits.
3 The name of the variable should be written in lowercase.(Eg. Rahul and rahul are distinct variables)

In Python, an identifier is a name given to entities like variables, functions, classes, etc. Identifiers are used to uniquely identify these entities in your code. Here are some key rules and examples to help you understand identifier naming:

Rules for Naming Identifiers

  1. Start with a Letter or Underscore: Identifiers must begin with a letter (a-z, A-Z) or an underscore (_).
  2. Followed by Letters, Digits, or Underscores: After the first character, identifiers can have any combination of letters, digits (0-9), or underscores.
  3. Case-Sensitive: Identifiers are case-sensitive, meaning myVariable and myvariable are considered different.
  4. No Reserved Keywords: Identifiers cannot be the same as Python’s reserved keywords (e.g., forwhileif).

Examples of Valid Identifiers

  • myVariable
  • _my_variable
  • variable123

Examples of Invalid Identifiers

  • 123variable (cannot start with a digit)
  • my-variable (hyphens are not allowed)
  • for (reserved keyword)

Example in Code

Here’s a simple example to illustrate valid and invalid identifiers:

# Valid identifiers
name = "Alice"
_age = 30
score123 = 95

# Invalid identifiers
123name = "Bob"  # SyntaxError: invalid syntax
my-variable = 50  # SyntaxError: invalid syntax
for = 10  # SyntaxError: invalid syntax

In this example:

  • name_age, and score123 are valid identifiers.
  • 123namemy-variable, and for are invalid identifiers due to the reasons mentioned.

Declaring Variable and Assigning Values

In Python, declaring a variable and assigning a value to it is straightforward. You simply use the assignment operator = to assign a value to a variable. Python is dynamically typed, so you don’t need to declare the type of the variable explicitly.

Example

Here’s a simple example to illustrate declaring variables and assigning values:

# Declaring and assigning values to variables
name = "Alice"  # name is a string
age = 30        # age is an integer
height = 5.7    # height is a float
is_student = True  # is_student is a boolean

# Printing the variables
print(name)     # Output: Alice
print(age)      # Output: 30
print(height)   # Output: 5.7
print(is_student)  # Output: True

In this example:

  • name is assigned the string value "Alice".
  • age is assigned the integer value 30.
  • height is assigned the float value 5.7.
  • is_student is assigned the boolean value True.

Multiple Assignments

You can also assign values to multiple variables in a single line:

x, y, z = 10, 20, 30
print(x, y, z)  # Output: 10 20 30

In this example, xy, and z are assigned the values 1020, and 30 respectively.

Changing Variable Values

x = 5
print(x)  # Output: 5

x = "Python"
print(x)  # Output: Python

Here, x is initially assigned the integer value 5 and later reassigned the string value "Python".

Object References

An object reference is a way to access an object stored in memory. When you create a variable and assign it a value, the variable name becomes a reference to the object in memory. This means that the variable name points to the location in memory where the object is stored.

Key Points

  1. Reference vs. Object: The variable name is a reference, and the actual data stored in memory is the object.
  2. Multiple References: Multiple variables can reference the same object.
  3. Dynamic Typing: Python variables can reference objects of different types at different times.

Example

Here’s a simple example to illustrate object references:

# Assigning a value to a variable
a = [1, 2, 3]  # 'a' references a list object

# Assigning the same reference to another variable
b = a

# Modifying the object through one reference
b.append(4)

# Both variables reflect the change
print(a)  # Output: [1, 2, 3, 4]
print(b)  # Output: [1, 2, 3, 4]

# Checking if both variables reference the same object
print(a is b)  # Output: True

In this example

  • a is a reference to a list object [1, 2, 3].
  • b is assigned the same reference as a, so both a and b point to the same list object.
  • Modifying the list through b also affects a because they reference the same object.
  • The is operator confirms that a and b reference the same object.

Another Example with Different Types

# Assigning different types to the same variable
x = 10       # 'x' references an integer object
print(x)     # Output: 10

x = "Hello"  # 'x' now references a string object
print(x)     # Output: Hello

In this example:

  • x initially references an integer object 10.
  • Later, x is reassigned to reference a string object "Hello".

This demonstrates Python’s dynamic typing, where the type of the object a variable references can change over time.

Object Identity

 Object identity refers to the uniqueness of an object in memory. Each object in Python has a unique identifier, which can be obtained using the id() function. This identifier is essentially the memory address where the object is stored.

Key Points

  1. Identity vs. Equality: Object identity is different from object equality. Two objects can be equal (i.e., have the same value) but have different identities (i.e., stored at different memory locations).
  2. is Operator: The is operator checks if two variables reference the same object (i.e., have the same identity).

Example

# Creating two different objects with the same value
a = [1, 2, 3]
b = [1, 2, 3]

# Checking equality
print(a == b)  # Output: True (they have the same value)

# Checking identity
print(a is b)  # Output: False (they are different objects)

# Getting the unique identifier (memory address) of the objects
print(id(a))  # Output: (some unique identifier)
print(id(b))  # Output: (a different unique identifier)

In this example:

  • a and b are two different list objects with the same value [1, 2, 3].
  • a == b returns True because the values are equal.
  • a is b returns False because a and b are different objects in memory.
  • The id() function shows that a and b have different unique identifiers.

Variable Names

Variable names are used to identify variables and store data. Here are some key rules and examples to help you understand how to name variables correctly :

  1. Start with a Letter or Underscore: Variable names must begin with a letter (a-z, A-Z) or an underscore (_).
  2. Followed by Letters, Digits, or Underscores: After the first character, variable names can contain letters, digits (0-9), or underscores.
  3. Case-Sensitive: Variable names are case-sensitive, meaning myVariable and myvariable are considered different.
  4. No Reserved Keywords: Variable names cannot be the same as Python’s reserved keywords (e.g., forwhileif).

Examples of Valid Variable Names

  • myVariable
  • _my_variable
  • variable123

Examples of Invalid Variable Names

  • 123variable (cannot start with a digit)
  • my-variable (hyphens are not allowed)
  • for (reserved keyword)

Example in Code

Here’s a simple example to illustrate valid and invalid variable names :

# Valid variable names
name = "Alice"
_age = 30
score123 = 95

# Invalid variable names
# 123name = "Bob"  # SyntaxError: invalid syntax
# my-variable = 50  # SyntaxError: invalid syntax
# for = 10  # SyntaxError: invalid syntax

In this example:

  • name_age, and score123 are valid variable names.
  • 123namemy-variable, and for are invalid variable names due to the reasons mentioned.

Naming Conventions

Python follows certain naming conventions to make code more readable:

  • Snake Case: Words are separated by underscores, and all letters are lowercase (e.g., my_variable_name).
  • Camel Case: Each word, except the first, starts with a capital letter (e.g., myVariableName).
  • Pascal Case: Each word starts with a capital letter (e.g., MyVariableName).

Example of Naming Conventions

# Snake Case
first_name = "John"
last_name = "Doe"

# Camel Case
firstName = "John"
lastName = "Doe"

# Pascal Case
FirstName = "John"
LastName = "Doe"

Leave A Comment

Your email address will not be published. Required fields are marked *