Python Variables

Python Variables

Python Variables, unlike other programming languages out there, doesn't use a command for declaring variables. The variable gets created the moment a value gets assigned to it.

x = 2
print(x)  # output is 2

y = 4
print(y) # output is 4

Since Python is a dynamically typed language you don't have to declare what data type a variable will be when you are assigning a value to it. Once a variable has been created the data type can be changed.
The five base data types are as follows: integer (int), float (float), boolean (bool), string(str), and bytes.

By using casting you can change the data type of a given value.

x = str(1)    # x will be '1'
y = int(2)    # y will be 2
z = float(3)  # z will be 3.0

Python provides a built-in function that comes in handy when you are not sure what kind of data type an operation or expression will yield. Check it out:

x = 25
y = "JLo"
print(type(x))   # output will be <class 'int'>
print(type(y))   # output will be <class 'str'>

A few additional notes about variables in Python will come in handy to know in the future. It doesn't matter what type of quotes you use to declare a string variable in Python but beware, you have to match the opening and closing quotes on the same variable in the same way.
Variable naming conventions are very important to adhere to when developing with Python. Variables cannot start with integers. They also cannot be separated by hyphens, but underscores are acceptable. They are forbidden from having any spaces in the name. Always remember that Python variables are case sensitive. (juuuuuuust trust me on this one.)

Well, that's it for now. More to come as I learn more about Python. Hopefully soon I'll be doing a post per day. Cheers, guys! Keep up the great work I see you all doing out there.

be safe.

 
Share this