Python Tutorial: Creating variables

Learn how to create variables in Python using the equals sign, with rules for naming them: start with a letter, use letters, numbers, and underscores only, and remember they're case-sensitive. The tutorial covers two data types, floats and strings, shows common mistakes like missing quotes, and demonstrates printing variables with the print function.
Chapters
Previously, you started writing Python code in the script editor and the console. You learned what a module is and how to import it. You also learned to simplify module names using an alias. In this lesson, you'll learn about variables, which help us reference a piece of data for later use. In the previous lesson, we told you about our kidnapped golden retriever, Bayes. To solve the mystery, let's start by filling out a missing puppy report. In order to file the report, we'll need to record some information about Bayes, such as his height and weight. In Python, we will represent each line from the missing puppy report with a variable. A variable gives us an easy to use shortcut to a piece of data. Whenever we use the variable name in our code, it will be replaced with the original piece of data. In this case, one of our variables is name and its value is Bayes. Another variable is height and Its value is 24. We define variables using an equals sign. When defining variables, we need to follow a few rules. Variables must start with a letter. You can use a capital letter, but we usually use lowercase after the first letter. We can use letters, numbers, and underscores in our variable name. We can't use special characters like exclamation points or dashes. Variable names are case sensitive, so these two different ways of typing my VAR would be different variables. On the left are some examples of valid variable names, and on the right are some examples of invalid variable names. Let's see what happens when we try to use an invalid variable name. The variable Bayes height is invalid because of the hyphen. When we try to enter it, we will receive a syntax error. Above the syntax error, we see the line of code that caused the problem and a caret that indicates approximately where the problem occurred. Variables come in many flavors. Two important flavors are floats and strings. Floats represent either integers or decimals. Strings represent text and can contain letters, numbers, spaces, and special characters. We define a string by putting either single or double quotes around a piece of text. It's easy to get errors when working with strings. If you get one, there are two likely causes. You might have forgotten to put quotation marks around your string. If you do this, Python will think that your string is a variable, and if that variable wasn't previously defined, you'll get a name error. Alternatively, you might have started your string with one type of quote and finished with a different type of. If you mix single and double quotes, you'll get a syntax error. If we want to know the current value of one of our variables, we can use print. We simply type the word print and put our variable name inside of the parentheses. When we execute the code, the value will appear in the console. Remember, the variable name is not a string, so we don't put it in quotes. Now that you know how to define and display variables, let's practice.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







