codewithronny logo
codewithronny logo
  • Category
    • Adsense
    • Blog
    • Boilerplate
    • CodeWithRonny
    • Course Review
    • How to
    • HTML Tutorial
    • JavaScript
      • JavaScript Tutorial
    • Laravel
    • Machine Learning
      • Machine Learning Tutorial
    • MySQL
      • MySQL Tutorial
    • Networking
    • PHP Tutorial
    • Python
      • Data Structure Using Python
      • Python Interview Question
      • Python Tutorial
    • Python Coding Interview Practice
    • Server
      • Share Hosting
    • Tech Celebrity
    • Web Technologies and Development
    • Wordpress
      • Theme
  • Information
    • Contact Us
    • About Us
    • Privacy Policy
    • Terms
    • Disclaimer
  • Code Editor
    • HTML Code Editor
    • Python Code Editor
Python Tutorial
Python TutorialPython VariablesVariables Name in PythonPython Variables - Multiple Value AssignmentPython Data Types

Variables Name in Python


Variable Names

In Python, a variable is like a storage box that holds information. It can have a short name, like a or b, or a more descriptive name, such as year, dayname, or total_area. Choosing a meaningful name for your variables helps you and others understand what information the variable is storing.

  • Variable names must begin with a alphabet letter (a to z, A to Z) or an underscore ( _ ).
  • After the first letter, you can use letters, numbers, or underscores in your variable names. Avoid using spaces or special characters.
  • Variable names can’t begin with a number. For example, 5total is not a valid variable name, but total5 is.
  • Python is case-sensitive, meaning year and Year would be considered different variables.
  • Don’t use Python reserved words (keywords) as variable names. For example, you can’t use if, else, while, etc., as variable names.
  • Variable names cannot contain spaces. If you need to represent multiple words, use underscores or capitalize each word (e.g., user_age or totalArea).
  • Python variable only contains Alphabet( a to z, A to Z), Numbers(0 to 9) and Underscore( _ ).
  • Pick names that give a clear idea of what the variable represents. For instance, instead of x, use something like total_count if the variable is counting something.

Here’s an example explains these rules:

# Valid variable names
year = 25
first_name = "Ronny"
total_area = 100.5

Invalid variable names:

# Invalid variable names (examples)
5total = 50   # Cannot start with a number
my-variable = 10  # Cannot contain hyphens
class = "Python"  # Avoid using Python keywords

By following these rules, you ensure that your Python code is readable, understandable, and free of naming conflicts or duplications.


Multi Word Variable Names

This is an method to construct variable in better human readable form.

Python gives us several method to specify the variables. For human more than one words variable can be difficult to read. That’s why python have several technique.

Snake Case(Underscore Separation)

In this method, words are separated by underscores, and the entire variable name is in lowercase.

total_volume = 100.5
user_age = 25
number_of_college_graduates = 5

This style, known as snake case, is widely used in Python and is recommended in the official PEP 8 style guide.

Camel Case

In camel case, the first word starts with a lowercase letter, and each next word starts with an uppercase letter.

totalVolume = 100.5
userAge = 25
numberOfCollegeGraduates = 5

Pascal Case(Upper Camel Case)

Similar to camel case, all the first letter of a word starts with an uppercase letter.

TotalVolume = 100.5
UserAge = 25
NumberOfCollegeGraduates = 5

Usage

Common Usage of Snake Case
  • Snake case is the most widely used naming convention in Python.
  • Typically used for variable names, function names, and module-level names.
Common Usage of Camel Case
  • Camel case is often used for naming classes in Python, especially when following Pascal Case (Upper Camel Case) for class names.
Common Usage of Camel Case
  • Pascal case is commonly used for class names in Python.






codewithronny logo
codewithronny logo

A Tutorial Cafe

  • Twitter
  • Facebook
  • Instagram
  • Linkedin
  • Linkedin
Company
    About Us
  • Contact Us
  • Career
Community
    Documentation
  • Faq
  • Sitemap
Explore
    Terms & Conditions
  • Privacy policy
  • Disclaimer
  • Affiliate Disclosure
  • Become an Author
  • How to guide
Contact


Copyrights ©2024 CodeWithRonny
  • GlobeLanguage Chevron
    • English
    • Hindi
  • Terms of use
  • Cookies
  • Sitemap