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

Python Variable Rules and Best Practices

Uploaded by

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

Python Variable Rules and Best Practices

Uploaded by

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

BASIC RULINGS OF VARIABLES AND VALUES OF PYTHON:

✅ 1. Variable Naming Rules


These are mandatory rules (you must follow them):

Start with a letter (a-z or A-Z) or an underscore (_)

Cannot start with a number

Only letters, numbers, and underscores allowed (a-z, A-Z, 0-9, _)

No special characters like @, $, %, etc.

No spaces allowed — use _ instead.

❌ Invalid:
python
Copy
Edit
2name = "John" # starts with number
my name = "John" # contains space
my-name = "John" # contains special character
✅ Valid:
python
Copy
Edit
name = "John"
_name = "John"
name2 = "John"
my_name = "John"
✅ 2. Variable Names are Case-Sensitive
age, Age, and AGE are different variables.

python
Copy
Edit
age = 18
Age = 25
print(age) # 18
print(Age) # 25
✅ 3. Don't Use Python Keywords
Python has reserved words like if, for, while, class, etc. Don't use them as
variable names.

❌ Example:

python
Copy
Edit
if = 5 # ❌ Invalid
✅ 4. Assigning Values
Use = to assign a value to a variable.

python
Copy
Edit
x = 10
name = "Alice"
is_active = True
✅ 5. Data Types
Here are some basic data types:

String – text inside quotes: "hello", 'hi'

Integer (int) – whole numbers: 5, -10

Float – decimal numbers: 3.14, 0.99

Boolean (bool) – True or False

python
Copy
Edit
name = "John" # string
age = 17 # integer
height = 5.9 # float
student = True # boolean
✅ 6. Strings must be in Quotes
You can use single ' or double " quotes.

python
Copy
Edit
greeting = "Hello"
place = 'World'
✅ 7. Use + to Join Strings
Only strings can be joined with other strings.

python
Copy
Edit
first = "I am"
age = "17"
print(first + " " + age + " years old")
⚠️ If age was a number (without quotes), it would cause an error:

python
Copy
Edit
age = 17
print("I am " + age) # ❌ Error: can't combine string and int
✅ Fix by converting the number:

python
Copy
Edit
print("I am " + str(age))
✅ 8. Dynamic Typing
You don’t declare data type. Python automatically figures it out.

python
Copy
Edit
x = 5 # int
x = "five" # string (Python allows changing type)
✅ 9. Multiple Assignments
You can assign multiple variables at once:
python
Copy
Edit
a, b, c = 1, 2, 3
✅ 10. Constants (by convention only)
Python doesn’t have true constants. But use uppercase names for values you don’t
want to change:

python
Copy
Edit
PI = 3.14
MAX_USERS = 100
🔁 Bonus Tip: Use type() to Check Data Type
python
Copy
Edit
x = 42
print(type(x)) # <class 'int'>

You might also like