4  Names and Variables

In programming, names and variables are fundamental concepts that form the building blocks of a program. They enable us to store, manipulate, and access data within our code, making it possible to write flexible and efficient programs. This chapter will delve into these essential concepts, exploring their significance and how they are used in Python.

4.1 Names

Names in programming are identifiers used to label and reference entities such as variables, functions, classes, and modules. They serve as a means to access stored data and functionality, making code more readable and maintainable. For instance, instead of repeatedly using a complex expression or value, we can assign it a name and refer to it using that name.

4.1.1 Rules for Naming

In Python, there are specific rules and conventions for naming identifiers:

  1. Alphabetic Characters and Underscores: Names must begin with a letter (a-z, A-Z) or an underscore (_), followed by letters, digits (0-9), or underscores.
  2. Case Sensitivity: Names are case-sensitive, meaning Variable, variable, and VARIABLE are distinct identifiers.
  3. Reserved Keywords: Names cannot be Python reserved keywords, such as if, else, for, while, class, def, etc.

4.2 Variables

Variables are names assigned to data values stored in memory. They act as containers holding information that can be manipulated and accessed throughout a program. Variables allow for dynamic and flexible data handling, making it possible to perform computations, store results, and manage state.

Variable Assignment

In Python, variable assignment is straightforward. Use the assignment operator (=) to assign a value to a variable:

x = 10
name = "Alice"
is_student = True

Variable Scope

The scope of a variable determines its accessibility within different parts of a program. There are two primary types of scope in Python:

  1. Global Scope: Variables defined outside any function (functions will be discussed in sec-functions) or block have global scope and can be accessed anywhere in the program.
  2. Local Scope: Variables defined within a function or block have local scope and can only be accessed within that function or block.
global_var = "I am global"

def my_function():
    local_var = "I am local"
    print(global_var)  # Accessible
    print(local_var)  # Accessible

my_function()
print(global_var)  # Accessible
print(local_var)  # Error: NameError

Constants

Constants are variables whose values are intended to remain unchanged throughout the program. While Python does not have built-in constant types, by convention, we use all uppercase letters to indicate constants:

PI = 3.14159
MAX_STUDENTS = 50

4.2.1 Examples and Applications

To solidify our understanding of names and variables, let’s explore some practical examples and applications:

Example: Simple Calculator
# Simple Calculator Program
num1 = 10
num2 = 5

# Performing arithmetic operations
sum_result = num1 + num2
difference = num1 - num2
product = num1 * num2
quotient = num1 / num2

print("Sum:", sum_result)
print("Difference:", difference)
print("Product:", product)
print("Quotient:", quotient)
Example: Temperature Conversion
# Temperature Conversion Program (Celsius to Fahrenheit)
celsius = 25
fahrenheit = (celsius * 9/5) + 32

print(celsius, "Celsius is", fahrenheit, "Fahrenheit")

Understanding names and variables is crucial for effective programming. Names provide a way to reference and access data, while variables store the data we work with. By adhering to naming conventions and understanding variable scope and types, we can write clearer, more maintainable code. As we progress, these fundamental concepts will underpin more complex programming tasks and techniques.

4.3 Exercises

Exercise 1: Variable Assignment

  1. Assign the value 50 to a variable named my_age and use print to display the variable.
  2. Assign the string "Hello, Python!" to a variable named greeting and use print to display the variable.
  3. Assign the boolean value False to a variable named is_raining and use print to display the variable.

Exercise 2: Variable Reassignment

  1. Assign the value 10 to a variable named number and use print to display the variable.
  2. Reassign the value of number to 20 and use print to display the variable.
  3. Reassign the value of number to the string "twenty" and use print to display the variable.

Exercise 3: Basic Arithmetic

  1. Create two variables a and b, and assign them the values 8 and 3 respectively.
  2. Perform the following operations and print the results:
    • Sum of a and b
    • Difference between a and b
    • Product of a and b
    • Quotient of a divided by b

Exercise 4: Temperature Conversion

  1. Create a variable fahrenheit and assign it the value 86.
  2. Convert the temperature to Fahrenheit using the formula: celsius = (fahrenheit - 32) * 5/9.
  3. Print the Celsius value.

Exercise 5: Constants

  1. Define a constant PI with the value 3.14159.
  2. Define a constant MAX_SPEED with the value 120.
  3. Print both constants.