Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele Python Naming Rules for Variables | Variables and Types in Python
Introduction to Python (dev copy)

bookPython Naming Rules for Variables

Variables should have descriptive names, such as is_user_logged_in, customer_full_name, or total_sales, to make code more readable and maintainable.

Concise names like x, y, or temp can also be used when necessary. The most important aspect is to follow the rules of naming.

Python will generate an error if you violate variable naming rules, such as starting a variable name with a number, using spaces or special characters, using reserved keywords, or starting with invalid characters.

There is also different naming conventions in Python, such as Camel Case, Snake Case (preferred in Python), Pascal Case, and Uppercase with Underscores (commonly used for constants).

# Camel Case 
numberOfActiveUsers = 120

# Snake Case  (preferred in Python)
number_of_active_users = 120

# Pascal Case
NumberOfActiveUsers = 120

# Uppercase with Underscores (for constants)
NUMBER_OF_ACTIVE_USERS = 120
question mark

Which of these would be valid variable names in Python?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 1.64

bookPython Naming Rules for Variables

Pyyhkäise näyttääksesi valikon

Variables should have descriptive names, such as is_user_logged_in, customer_full_name, or total_sales, to make code more readable and maintainable.

Concise names like x, y, or temp can also be used when necessary. The most important aspect is to follow the rules of naming.

Python will generate an error if you violate variable naming rules, such as starting a variable name with a number, using spaces or special characters, using reserved keywords, or starting with invalid characters.

There is also different naming conventions in Python, such as Camel Case, Snake Case (preferred in Python), Pascal Case, and Uppercase with Underscores (commonly used for constants).

# Camel Case 
numberOfActiveUsers = 120

# Snake Case  (preferred in Python)
number_of_active_users = 120

# Pascal Case
NumberOfActiveUsers = 120

# Uppercase with Underscores (for constants)
NUMBER_OF_ACTIVE_USERS = 120
question mark

Which of these would be valid variable names in Python?

Select the correct answer

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 2. Luku 2
some-alt