Python Basics: Printing Variables and Understanding Output Differences 🖨️
📋 Printing Multiple Variables
In Python, you can print multiple variables using a single print()
statement. This simplifies the display of values:
c = "Shivank"
a = "Agarwal"
print(c, a) # Output: Shivank Agarwal
Python automatically separates the values with a space, enhancing readability.
🔍 Difference Between print(c, a)
and c, a
print(c, a)
and c, a
Using
print(c, a)
: Displays values directly without additional formatting:c = "Alex" a = "Doyle" print(c, a) # Output: Alex Doyle
Using
c, a
directly (in interactive environments like Jupyter Notebook or Colab): Displays the variables as a tuple:c, a # Output: ('Alex', 'AgarDoyleal')
The key takeaway: print()
is for formatted output, while just writing c, a
shows the tuple representation.
🛠️ Why Use Variables?
Variables act as placeholders for data, simplifying your code by avoiding repetitive typing. For example:
# Instead of repeating values:
print(10 + 20)
print(10 * 20)
# Use variables:
x = 10
y = 20
print(x + y) # Output: 30
print(x * y) # Output: 200
This approach improves code clarity and reusability.
🔤 Variable Naming Rules
Follow these guidelines to name variables effectively:
✅ Allowed Characters:
Letters (a-z, A-Z)
Digits (0-9) – but cannot start with a digit.
Underscores (
_
)
❌ Not Allowed:
Special characters like
@
,#
,-
,$
, etc.Starting variable names with a digit.
🖍️ Examples:
Valid:
name = "Alice"
age = 30
_score = 95
total2 = 100
Invalid:
2total = 100 # Starts with a digit
first-name = "Bob" # Contains a hyphen
@age = 25 # Contains special character
🧠 Case Sensitivity:
Variable names are case-sensitive:
age = 25
Age = 30
print(age) # Output: 25
print(Age) # Output: 30
✍️ Readable Names:
Use meaningful names for clarity:
first_name = "John"
last_name = "Doe"
🔄 Assigning Multiple Variables
You can assign the same value to multiple variables:
a = b = c = 10
print(a, b, c) # Output: 10 10 10
⌨️ Taking User Input with input()
input()
Interactive programs often use the input()
function. Here’s how:
🔹 Basic Input:
name = input()
print(name)
The program waits for user input, which is then stored in the variable name
.
🔹 Input with a Prompt:
name = input("Enter your name: ")
print("Hello", name)
Output:
Enter your name: Alice
Hello Alice
🔹 Important Note on Data Types:
Input values are always strings, even if the user enters numbers:
age = input("Enter your age: ")
print(type(age)) # Output: <class 'str'>
🔹 Converting Input to Numbers:
To work with numbers, explicitly convert the input:
age = int(input("Enter your age: "))
print(type(age)) # Output: <class 'int'>
For decimal numbers, use float()
.
📝 Common Printing Mistakes
Enclosing a variable name in quotes prints it as a string:
name = "Alice" print("name") # Output: name print(name) # Output: Alice
🧩 Example: Putting It All Together
# Asking for user input
first_name = input("Enter your first name: ")
last_name = input("Enter your last name: ")
# Concatenating strings
full_name = first_name + " " + last_name
# Displaying the result
print("Hello,", full_name)
Output:
Enter your first name: Shivank
Enter your last name: Agarwal
Hello, Shivank Agarwal
🔑 Recap of Key Points
Printing Multiple Variables: Use
print(var1, var2)
for clear output.Variable Naming Rules:
Use letters, digits, and underscores.
Avoid starting with digits or using special characters.
Variable names are case-sensitive.
Taking User Input:
Use
input()
for interaction.Convert input to
int
orfloat
when needed.
Avoid Printing Mistakes: Ensure variables aren’t enclosed in quotes unless you intend to print their names.
🚀 Next Steps
In the next section, we’ll explore operators and expressions, delving into Python’s powerful tools for performing calculations, comparisons, and logical operations. Stay tuned! 😊
Last updated