Skip to main content
Practice

How to Name Variables and Functions Sensibly

In programming, it is recommended to follow consistent Naming conventions when deciding on names for variables and functions.

Programs cannot interpret words with spaces as a single object when used in variable or function names. Thus, spaces cannot be used in these names.

There are several naming conventions for handling spaces, and in Python, the snake_case and camelCase conventions are predominantly used.


Snake Case

Snake Case refers to a naming convention where words are connected by underscores (_) instead of spaces.

The appearance of variables and function names using this convention resembles a snake, hence the name Snake Case.

In Python, Snake Case is primarily used for naming variables and functions.

Example of snake_case
my_name = "CodeFriends" # Variable name

def my_function_name(): # Function name
print("Hello, world!")

Variable and function names typically start with lowercase letters, and multiple words are connected using underscores.

In the above example, the variable name that holds My Name is declared as my_name with an underscore between my and name.

Similarly, the function name, My Function Name, is declared as my_function_name.


Camel Case

Camel Case connects words by capitalizing the first letter of each word and is predominantly used in Python for naming classes (custom data structures).

Classes are a fundamental concept in programming and will be thoroughly covered in future lessons.

Camel Case comes in two forms: lowerCamelCase, where the first letter is lowercase, and UpperCamelCase (or PascalCase), where the first letter is capitalized.

The naming convention resembles a camel, hence the name Camel Case.

Example of camelCase
# Example of UpperCamelCase
class UserInfo: # Class name: UserInfo
# Define a class with age and name attributes
def __init__(self, name, age):
self.name = name
self.age = age


# Example of lowerCamelCase (rarely used in Python)
def myMethodName():
...

Coding Exercise

Try entering the highlighted code user_age = 25 in the exercise screen.

The value 25 stored in the user_age variable is passed to the age attribute in the UserInfo class.

Detailed explanations about classes will be provided in subsequent lessons.

Want to learn more?

Join CodeFriends Plus membership or enroll in a course to start your journey.