This article shows basics of variables in Python.
Proper understanding of variable naming conventions improves code readability.
Contents
- Variable name conventions in Python
- Prohibitions of Variable name
- Variable Declaration in Python
- Variable types
- How to check variable types
- How to convert variable types
- 【Appendix】Naming Conventions other than variable
sponsored link
Variable name conventions in Python
In the beginning, I show variable name convention in Python.
Basics of variable name conventions
The following three types of characters can be used in the variable name.
・Alphabet
・Numbers
・'_' : Underscore
Examples of variable names are as follows
test = 100
test1 = 100
test_1 = 100
Simple, but this is the only variable name conventions.
Variable names must be determined while following these conventions and avoiding prohibitions.
sponsored link
Prohibitions of Variable name
There are three main prohibitions on naming variables.
・Cannot use "number" at the "beginning" of a variable
・Cannot use Reserved Keywords
・Cannot use Symbols
I show these three prohibitions in order.
--- Cannot use "number" at the "beginning" of a variable ---
### syntax error
1a = 100
--- Cannot use Reserved Keywords ---
"if", "for", and other strings that are reserved keywords in Python cannot be specified as variable names.
### syntax error
if = 1
The following are examples of reserved keywords that cannot be used as variable names.
if
True
import
as
return
not
etc.
--- Cannot use Symbols ---
### error
a&b = 1
The following are examples of symbols that cannot be used as variable names.
@
.
+
-
*
%
/
etc.
sponsored link
Variable Declaration in Python
Other programming languages, such as C, require "variable declaration and variable type declaration" when using variables.
Python, however, does not require this "variable declaration".
In Python, the type of variable is automatically determined.
Comparison of variable declaration in C and Python
--- C ---
int a
a = 1
--- Python ---
a = 1
Python is easier.
However, be aware that dynamic variable declaration in python can also cause bugs.
Examples of bugs in Python
Here is an example of a bug caused by dynamic variable declaration in Python.
Variables of numeric type are overwritten with string.
num = 100
fruit = 'Apple'
num = fruit
sum = num + 1
# >> TypeError: can only concatenate str (not "int") to str
"num" was initially variable of numeric type, but has become string type in the process.
Therefore, addition will cause "TypeError".
sponsored link
Variable types
Here are the main data types in Python.
Data Type | Description | Example | |
---|---|---|---|
Numeric | Interger | Numeric type without decimal point | num = 100 |
float | Numeric type with decimal point | num = 1.14 | |
Complex number | Denote imaginary units with j | a = 1 + 2j | |
Sequence Type | Strings | enclose with ' ' or " " | fruit = 'Apple' |
Tuple | ・Defined with ( ) ・Separate with , ・Elements cannot be changed. | (1, 2, 3) | |
List | ・Defined with [ ] ・Separate with , ・Elements can be changed. | [1, 2, 3] | |
Set | ・Defined with { } ・Duplicate elements are ignored. ・Elements are not ordered. | set1 = {1, 2, 3, 3, 1, 2} print(set1) # {1, 2, 3} | |
Dictionary | ・Defined with { } ・Combination of key and value | price = {"apple":100, "banana":200, "orange" : 100} | |
Boolean | ・True, False | is_ok = True |
I give a supplementary explanation about sequence type.
Sequence type is the type that stores multiple elements.
String type is also a member of the sequence type since it contains multiple characters.
fluit = 'Apple'
for char in fluit :
print(char)
###Output###
#A
#p
#p
#l
#e
The elements of the string "Apple" are taken one by one and displayed.
You can see that string type is also sequence type because the elements can be taken out one by one.
sponsored link
How to check variable types
The data type can be checked by using type() function.
num = 100
fruit = 'Apple'
is_ok = True
print(num, type(num))
print(fruit, type(fruit))
print(is_ok, type(is_ok))
###Output###
# 100 <class 'int'>
# Apple <class 'str'>
# True <class 'bool'>
sponsored link
How to convert variable types
Data types can be converted.
Below are the main type conversion functions.
int() : Conversion to integer type.
float() : Conversion to float type.
str() : Conversion to string type.
num = "1"
print(type(num))
###Output###
#<class 'str'>
num = int(num)
print(type(num))
###Output###
#<class 'int'>
sponsored link
【Appendix】Naming Conventions other than variable
As a supplement, I also show naming conventions other than variable.
Item | Rule | Example |
---|---|---|
Package | ・All lowercase ・Underscore is deprecated | tqdm, requests |
Module | ・All lowercase ・Underscore is available | sys, os |
Class | ・First letter is uppercase ・Delimiter : uppercase | MyFavoriteClass |
Exception | ・First letter is uppercase ・Delimiter : uppercase | MyError |
Method | ・All lowercase ・Delimiter : underscore | my_favorite_method |
Function | ・All lowercase ・Delimiter : underscore | my_favorite_funcion |
Variable | ・All lowercase ・Delimiter : underscore | my_favorite_instance |
Constant | ・All uppercase ・Delimiter : underscore | MY_FAVORITE_CONST |
sponsored link