Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Python Variables: Declaring and Managing String Types

What Is a Variable in Python?

A Python variable is a reserved memory location that stores a value. In a program, a variable supplies data to the computer for processing.

Python Variable Types

Every value in Python has a datatype—numbers, lists, tuples, strings, dictionaries, and more. Variable names can be any valid identifier such as a, aa, or abc.

How to Declare and Use a Variable

Below is a simple example that declares a variable a and prints its value.

a=100
print(a)

Re‑Declare a Variable

Python lets you re‑assign a variable after it has been created. The following examples demonstrate this in both Python 2 and Python 3.

Python 2:

# Initial declaration
f = 0
print f
# Re‑assignment
f = 'guru99'
print f

Python 3:

# Initial declaration
f = 0
print(f)
# Re‑assignment
f = 'guru99'
print(f)

Python Variables: Declaring and Managing String Types

String Concatenation with Variables

Unlike Java, Python does not automatically convert numbers to strings during concatenation. Attempting to concatenate a string and an integer raises a TypeError. Convert the number to a string first:

a='Guru'
b=99
print(a+str(b))  # Outputs: Guru99

Python Variables: Declaring and Managing String Types

Local vs. Global Variables

Variables can be either local (limited to a function) or global (accessible throughout the module). The following examples illustrate the difference.

Python 2 (without the global keyword):

f = 101
print f

def someFunction():
    f = 'I am learning Python'
    print f

someFunction()
print f

Python 3 (without the global keyword):

f = 101
print(f)

def someFunction():
    f = 'I am learning Python'
    print(f)

someFunction()
print(f)

Python Variables: Declaring and Managing String Types

Using the global keyword allows a function to modify a global variable:

Python 2:

f = 101
print f

def someFunction():
    global f
    print f
    f = 'changing global variable'

someFunction()
print f

Python 3:

f = 101
print(f)

def someFunction():
    global f
    print(f)
    f = 'changing global variable'

someFunction()
print(f)

Python Variables: Declaring and Managing String Types

Deleting a Variable

To remove a variable from memory, use the del keyword. After deletion, accessing the variable raises a NameError:

f = 11
print(f)

del f
print(f)  # Raises NameError

Python Variables: Declaring and Managing String Types

Summary

Python

  1. Master C# Variables & Primitive Data Types: A Complete Guide
  2. Mastering Python Data Types: A Practical Guide
  3. Master Python Strings: Creation, Formatting, and Manipulation
  4. C++ Variables and Data Types: Expert Guide to int, double, char, string, bool
  5. C Strings Made Easy: Declaration, Input, Output, and Library Functions
  6. Understanding Java Variable Types: A Comprehensive Guide
  7. Understanding Variables in C: Types, Naming Rules, and Memory Management
  8. Understanding Variables in C#: Types, Storage, and Usage
  9. Python Variable Types: Understanding and Using Data Types
  10. Mastering Python Strings: Creation, Access, and Manipulation