Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Notice Board

Per partecipare al corso di Fondamenti di programmazione 2023-24 loggatevi e attivatelo nella vostra pagina dei corsi preferiti. A quel punto il corso appare nel menù personale cliccando sul proprio avatar. Per i materiali degli anni precedenti seguite lo stesso metodo.

To join the Programming/Lab 2023-24 course, log-on and select it on the my courses page. It will appear on the personal menu of your avatar. For earlier years use the same method.

What is the convention for naming variables in python?

c
charlieHeron (1380 points)
7 19 26
in Programming in Python by (1.4k points)
retagged by
Eg: my_variable, myVariable, etc

8 Answers

yGifoom (2160 points)
0 0 20
by (2.2k points)
There is no actual consensus on this, it really depends on your coding style:

myVariable

my_variable

myvar

Etc etc.....

just be consistent with it, your code will be more tidy this way
Julio Zenelaj (3190 points)
1 8 32
by (3.2k points)
In Python I use myVariable for variables and objects, my_function for functions, MyClass for classes.
francesco.calzona (5210 points)
5 20 81
by (5.2k points)

I would not say there is a naming convention in that sense, but there are many variable names that you would never use, even though they might be allowed, such as starting a variable with a capital letter or a _.

But in general, I consider consistency more important.

J
Jad (2690 points)
6 27 38
by (2.7k points)
The convention is to name the variables in a way that relates to what you are talking about. For example if you want from the user to input calories, just do

calories=int(input())
nand (6230 points)
1 4 20
by (6.2k points)
In the professional setting the most popular convention is my_varible but, as others have pointed out, it truly depends on your style.
r0mania (2580 points)
1 2 13
by (2.6k points)

Do whatever you want, the important thing is that you don't get confused when you use them.

Diapa (4690 points)
0 1 28
by (4.7k points)

It depends on how you are used to, but remember that:

  • you can't use numbers at the beginning of the name of the variable;
  • you can't use the sign - in the name of the variable.

You can't name then a variable like 2ndNumber or number-2.

This can't be done since Python will interpeter them as a mathematical operation. For the rest, you're free to name a variable any way you want. Remember that the names should follow a certain criteria, not only for you but also for other people that would need to access and/or edit your code, so that it won't be that hard to understand what you wrote

F
FrancescoDB (240 points)
0 0 2
by (240 points)
edited by

So, there are a few naming conventions out there. We have camelCase, snake_case, PascalCase just to name a few of them. In the end it's always up to you to decide which one you prefer. I would just suggest you to find the naming convention that fits you the best and then be consistent with it when naming things in your code. By doing this your code is going to be a little bit more elegant and consistent.

Personally I use snake_case for variables and functions and PascalCase for classes (we will learn what they are in the future). 

We'll also learn that trailing underscores have a special meaning for the python interpreter (find out more).

If you're interested in finding out more about these naming conventions and if you would like to learn the "pythonic" way of naming things I would suggest you to read the PEP-8 document. This is the paragraph related to naming conventions. This document, written by Guido van Rossum himself and other two collaborators, is aimed at giving coding conventions for code written in Python.

But in the end it's always up to you, go with what you feel fits you the best.

angelo.spognardi (8190 points)
77 156 226
by (8.2k points)

Dear Charlie that is a very good question! I will discuss it during our next lecture.

The official guidelines (written by the "father" of Python) are included in a document called PEP 8. I think all of you should give it a read.

You can refer to this other resource if you want a quicker overview.