0% found this document useful (0 votes)
4 views3 pages

Python Variables: Naming and Best Practices

This document provides an overview of variables in Python, including their creation, naming rules, and best practices. It explains how to assign multiple variables and the types of data they can store, emphasizing Python's dynamic typing. Additionally, it offers guidelines for naming conventions and common practices to follow when working with variables.

Uploaded by

vijayamanabala25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Variables: Naming and Best Practices

This document provides an overview of variables in Python, including their creation, naming rules, and best practices. It explains how to assign multiple variables and the types of data they can store, emphasizing Python's dynamic typing. Additionally, it offers guidelines for naming conventions and common practices to follow when working with variables.

Uploaded by

vijayamanabala25
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Variables

1. Introduction to Variables:
Variables are used to store data values. In Python, variables are created when you assign a
value to them, and they don’t require explicit declarations.

Example:

x = 5
y = "Hello"

Variable Naming Rules

1. Names must start with a letter or an underscore _.


2. Names cannot start with a number.
3. Allowed Characters: Letters, numbers, and underscores (_).
4. Case-Sensitive: Variables name, Name, and NAME are treated as different.

Valid Examples:

student_name = "Alice"
age = 21

Invalid Examples:

1name = "Alice" # Starts with a number


student-name = "Alice" # Contains hyphen

Best Practices: Use descriptive names that clearly represent the data they store (e.g.,
total_price instead of x).

Assigning Multiple Variables


Python allows assigning values to multiple variables in a single line or assigning the same value
to multiple variables.

Assigning Multiple Values:


x, y, z = 1, 2, 3
Assigning the Same Value:

x = y = z = "Python"

Variable Output

Use the print() function to display variable values. You can print variables along with text by
using commas to separate values.

Example:

name = "Alice"
print("Hello,", name) # Output: Hello, Alice

Variable Types in Python


Variables in Python can store various data types, including numbers, strings, lists, dictionaries,
and more.

Examples:

age = 25 # Integer
name = "Alice" # String
height = 5.8 # Float
is_student = True # Boolean

Python is dynamically typed, meaning variable types are determined at runtime and can be
changed by reassigning the variable.

Common Practices with Variables

● Use lowercase letters and underscores for variable names (student_name).


● Avoid using Python keywords (e.g., for, if, def) as variable names.
● Use comments to clarify the purpose of variables if necessary.

You might also like