Variables in Python [Explained]
Contents
Create variables in python
- There is no command or specific syntax for variables in python.
- Variables are created when you assign a value to it:
a = 5
b= 2
c = "Hello, World!"
print(a)
print(b)
print(c)
- You don’t need to specify the type of the variable before assigning it.
- In the example above ‘a’ variable is an integer and ‘c’ is a string and both are assigned in the same way.
Assign type to variable in python
you can assign a data type to a variable in python like this:
x = str(3) # string
y = int(3) # integer
z = float(3) # float , it's basically converting from integer to float.
Get the type of a variable in Python:
x = str(3)
y = int('3')
print(type(x))
print(type(y))
Output:
<class 'int'>
<class 'str'>
Unaccepted variable names in Python:
- No numbers in variable start.
- no dash ‘-‘.
- no spaces.
2myvar = "John" X
my-var = "John" X
my var = "John" X