Bird
Raised Fist0
Pythonprogramming~10 mins

Naming rules and conventions in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a valid variable name in Python.

Python
my[1] = 10
Drag options to blanks, or click blank then click option'
A1var
B_var
C-var
Dvar!
Attempts:
3 left
💡 Hint
Common Mistakes
Starting variable names with a digit
Using special characters in variable names
2fill in blank
medium

Complete the code to follow Python naming conventions for constants.

Python
[1] = 3.14
Drag options to blanks, or click blank then click option'
ApiValue
BPi_Value
Cpi_value
DPI_VALUE
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for constants
Not using underscores to separate words
3fill in blank
hard

Fix the error in the variable name to make it valid.

Python
user[1]name = 'Alice'
Drag options to blanks, or click blank then click option'
A-name
B name
C_name
D1name
Attempts:
3 left
💡 Hint
Common Mistakes
Including spaces in variable names
Starting variable names with digits
Using hyphens in variable names
4fill in blank
hard

Fill both blanks to create a dictionary comprehension with valid variable names and a condition.

Python
{word: len(word) for word in words if len(word) [1] [2]
Drag options to blanks, or click blank then click option'
A>
B<
C3
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid operators
Using variable names instead of numbers in conditions
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension with uppercase keys, values, and a condition.

Python
result = [1]: [2] for word in words if len(word) [3] 4
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using length as key instead of uppercase word
Using wrong comparison operator
Mixing keys and values

Practice

(1/5)
1. Which of the following is a valid variable name in Python?
easy
A. my-variable
B. 2ndVariable
C. my_variable
D. my variable

Solution

  1. Step 1: Check the first character of each name

    Python variable names must start with a letter or underscore, not a number or special character.
  2. Step 2: Identify invalid characters

    Names cannot contain spaces or hyphens. Only letters, digits, and underscores are allowed.
  3. Final Answer:

    my_variable -> Option C
  4. Quick Check:

    Valid variable name = my_variable [OK]
Hint: Variable names start with letter/underscore, no spaces or hyphens [OK]
Common Mistakes:
  • Starting variable name with a number
  • Using hyphens instead of underscores
  • Including spaces in variable names
2. Which of these is the correct way to name a class in Python?
easy
A. MyClass
B. my_class
C. myclass
D. myClass

Solution

  1. Step 1: Recall class naming convention

    Class names in Python use CapitalizedWords style (PascalCase), starting with uppercase letters.
  2. Step 2: Compare options to convention

    Only 'MyClass' follows CapitalizedWords style correctly.
  3. Final Answer:

    MyClass -> Option A
  4. Quick Check:

    Class name style = CapitalizedWords [OK]
Hint: Class names use CapitalizedWords (PascalCase) [OK]
Common Mistakes:
  • Using lowercase or underscores for class names
  • Starting class names with lowercase letters
  • Mixing camelCase with underscores
3. What will be the output of this code?
_count = 5
Count = 10
print(_count + Count)
medium
A. NameError
B. 15
C. SyntaxError
D. 510

Solution

  1. Step 1: Understand variable names and case sensitivity

    Python variable names are case-sensitive, so '_count' and 'Count' are different variables.
  2. Step 2: Calculate the sum of the two variables

    _count = 5 and Count = 10, so 5 + 10 = 15.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    5 + 10 = 15 [OK]
Hint: Variable names are case-sensitive; add values accordingly [OK]
Common Mistakes:
  • Ignoring case sensitivity and treating variables as same
  • Expecting concatenation instead of addition
  • Assuming syntax or name errors
4. Find the error in this variable name:
user-name = 'Alice'
medium
A. Variable names cannot contain hyphens
B. Variable names cannot start with a letter
C. Variable names cannot contain underscores
D. Variable names cannot be lowercase

Solution

  1. Step 1: Identify invalid characters in variable names

    Hyphens are not allowed in Python variable names because they are interpreted as minus signs.
  2. Step 2: Confirm other options are incorrect

    Variable names can start with letters, contain underscores, and be lowercase.
  3. Final Answer:

    Variable names cannot contain hyphens -> Option A
  4. Quick Check:

    Hyphens not allowed in names [OK]
Hint: No hyphens in variable names; use underscores instead [OK]
Common Mistakes:
  • Using hyphens instead of underscores
  • Thinking variable names can't start with letters
  • Confusing underscores with invalid characters
5. You want to create a dictionary where keys are variable names and values are their types. Which naming style is best for the keys?
variables = { 'UserName': str, 'user_age': int, '2ndUser': str }
Which keys follow Python naming rules and conventions?
hard
A. 'user_age' only
B. All keys are valid
C. 'UserName' only
D. 'UserName' and 'user_age' only

Solution

  1. Step 1: Check each key for naming rules

    'UserName' starts with a letter and uses CapitalizedWords (good for class names). 'user_age' uses lowercase with underscore (good for variables). '2ndUser' starts with a number, which is invalid.
  2. Step 2: Identify keys following naming conventions

    'UserName' and 'user_age' follow naming rules; '2ndUser' does not.
  3. Final Answer:

    'UserName' and 'user_age' only -> Option D
  4. Quick Check:

    Keys must start with letter; numbers not allowed [OK]
Hint: Keys must start with letter; numbers not allowed [OK]
Common Mistakes:
  • Allowing keys starting with numbers
  • Ignoring naming conventions for readability
  • Assuming all strings are valid variable names