As with all other languages, Python has variables that can be defined to hold data. As we learn about python variables, it is important to remember a few fundamental things about Python: It is dynamically typed, so variables can type - and Python infers the type of variables from their content. change It is strongly typed, so types cannot change in unexpected ways - i.e. we can't add a string and a number together like we can in . Javascript How to define variables in Javascript Let's start learning about how we define variables. First, make a file called , and let's start experimenting. variables.py Standard variable definition Unlike Javascript, or other languages, there is no need to use a keyword to define a variable. For example, if we want to define a variable with a value (string) - then we just do the following to create the variable, and then print it: x Hello World x = "Hello World" print(x) We can test this out in our file by saving this, and running from terminal, when we are in the directory where is stored. If, for whatever reason, the command cannot be found, . variables.py python variables.py variables.py python you can learn how to resolve that here As in other languages, we have multiple types. Here are a few examples: w = "Hello World" # String x = 1 # Int y = 2.5 # Float z = True # Boolean print(w, x, y, z) One difference from some other languages, is when defining a boolean, we use capital letters - so you have to write . We can find out the types of any variable using the function: True type w = "Hello World" # String x = 1 # Int y = 2.5 # Float z = True # Boolean # Will return <class 'int'> print(type(x)) Defining multiple variables at once We can also define multiple variables at once in Python using the comma notation. So, if we wanted to define , , , and on one line, we could do it like this: w x y z w, x, y, z = ("Hello World", 1, 2.5, True); print(w, x, y, z); Concatenating Variables Just like other languages, we can concatenate variables if needed. We create a string concatenation like so: location = "World" print("Hello " + location) : as previously mentioned, we can only concatenate strings of the same type. So if we try to concatenate an instead, we'll get an error: Note int day = 6 # Throws an error: TypeError: can only concatenate str (not "int") to str print("It is day " + day) To resolve this, we can use type casting. Type Casting So, as mentioned, we can't concatenate variables of differing types. If we want to force an to be so we can concatenate it with another , we need to use casting. To do that, we use functions like , which will force it's content to cast to a string. int string string str() day = 6 # Throws an error: TypeError: can only concatenate str (not "int") to str print("It is day " + str(day)) Other types of casting As well as , there are 2 other casting functions in Python: str() - creates an integer from a string, integer, or float. int() - creates a float from a string, integer or float. float() These can easily be used in your code just like we used above. Here are some examples: str() x = 5 # Int y = 2.5 # Float z = "Hi" # String a = "5" # String xFloat = float(x) yInt = int(y) aInt = int(a) # Will return 5.0 2 5 print(xFloat, yInt, aInt) : if you try to convert above to an or , it will throw an error - since the text cannot be interpreted as a float or integer. The error we'd get looks like this: Note z int float "Hi" ValueError: invalid literal for int() with base 10: 'Hi' Conclusion Python variables work in the same way as most other languages, but there are some differences if you are coming from a different background. In this guide, we've covered the fundamentals. If you want to stay in the loop, for more python tutorials. follow me on twitter Also Published Here