Lecture

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.

Since spaces aren’t allowed in variable or function names, naming conventions are used to combine multiple words into a single identifier.

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.

This style gets its name because the connected words with underscores resemble the shape of a snake.

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 commonly used in Python to name classes, which are user-defined 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.

In this example, the value 25 stored in user_age will be used as the age attribute when creating an instance of the UserInfo class.

Detailed explanations about classes will be provided in subsequent lessons.

Mission
0 / 1

What naming convention is primarily used for writing variable and function names in Python?

Words are connected with underscores based on whitespace in .
snake_case
CamelCase
PascalCase
kebab-case

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result