Python
Python is a dynamic programming language, and if you have some time programming, or you are just starting, maybe you'v heard that naming variables is very important, but it’s not only giving long names to your variables, they have to be meaningful, they have to tell you what’s the program doing, or where are you going to use it.
Python conventions PEP8
Python uses language conventions for many things and one of them is to naming variables, these conventions are grouped in the PEP 8 Style Guide for Python Code as they say:
“This document gives coding conventions for the Python code comprising the standard library in the main Python distribution.”
Reserved words or keywords
There are words that you can’t use they are called reserved keywords these keywords can be listed inside python by importing the module keyword and printing the function keyword.kwlist.
These are some of the keywords in Python:
All of these words can’t be used as variable names, you can check out if a word is a keyword by using the function keyword.iskeyword() and passing the word inside the parentheses, for example:
like in keyword.iskeyword(‘else’) this will return a Boolean, in this case will be True.
Tips for naming variables in Python
Some tips to naming variables in Python are:
- Using numbers and making them more descriptive as you can.
- Don’t use special characters like @ or $.
- Variable names can’t start with numbers.
- To join words in variable names, you can use camel casing, but the convention is to use underscores like variable_name.
In python you don’t need to declare explicitly that something is a variable as in other languages, but when you write the name of a variable you can use some conventions to communicate what type of variable is.
The variable types are:
Public variables: these are the “normal” variables, you declare them by writing the variable name then an equal sign and what we want to store. e.g. greeting = ‘hello’
Private variables: these are variables that aren’t part of the public part of the program, is commonly used with APIs, in Python you can access a private variable any time, but what a private variable tells you is that if you change the content you can break the program or create malfunctions in it. e.g _age = 20.
Constants: By convention are all upper case, are values that are not going to change over time. e.g. PI = 3.14159.
“Super private” variables: totally private or please don’t touch me variables these starts with a double underscore. If you change anything inside these variables, you probably are going to break
all your program. e.g __really_important_number = 15.
This was a quick summary of naming variables and variables types, Python is a great programming language and can be used in many fields, tell me, have you ever saw any of these variables type? do you write Python code? Which is your favorite programming language?
A big thanks to ThisIsEngineering from Pexels for the cover photo
Top comments (1)
Interesting