From the course: Programming Foundations: Fundamentals

Variables across languages - Python Tutorial

From the course: Programming Foundations: Fundamentals

Variables across languages

- Depending on the programming language that you're working with, variables can behave in different ways. Some languages require that you define your variables and their types before you can use them, like Java, C#, and C++. For example, if we want to store the name of a cookie in Java, we would have to first declare that it's going to be of type string and then give our variable a name, cookie. When we run this code, we get sugar as our output. Since we've defined our variable as type string, we can't then later decide to set this variable to a different type. Let's say an integer. When we compile our program, we'll receive an error because Java is a strict programming language. It doesn't allow you to reassign data types in this way. But if we were to compare this to a more relaxed language like Python, we don't need to declare a variable's type before using it. Here, we declare the variable and use it at the same time. Notice how we only provide the name, not the type. But when we run this code, the Python interpreter was able to figure out the type based on the value that we provided. What's more? If we wanted to provide a value of a different type, Python has no issues with that, either. It just figures it out and keeps on moving. There are pros and cons to both styles. The more relaxed languages tend to be more flexible and result in less code, whereas with more strict languages, you have fewer surprises when the code is actually run. If you created an account balance variable as an integer, you don't have to worry about it later being set to fill, for example. Like all things in programming, there are a few rules that we have to follow when working with variables in Python. First, variable names should contain only letters, numbers, and underscores. And even though numbers are allowed, the name shouldn't start with a number. 3blindmice would not be a valid name, for instance. Second, spaces are not allowed in the name. Account space balance would be invalid, but account_balance would be just fine. Third, it's important to know that names are case sensitive. Capital C Cookies is not the same as lowercase c cookies. Python considers them to be two different locations in memory. And finally, variables cannot be keywords. These are reserved words that mean something special to the Python interpreter, like and, break, or try. We'll learn more about keywords shortly. However, if you're ever curious about the complete listing of keywords in the Python language, you can run the following code snippet to get a listing of all of the keywords. In general, you want to use short, descriptive names that make your code easy to understand. Naming things takes practice. In fact, it's often cited as one of the hardest problems in computer science. Keep that in mind and be patient with yourself as you get used to working with variables.

Contents