From the course: Python Essential Training

Variables and types

- [Instructor] Variables are the most basic building block of a program that are units that are assigned some value. For instance, I can take the variable x and assign it the value 5. The equal sign, again, means put the value 5 into the variable x. And so we call this equal sign an assignment operator. And I can run the cell in Jupyter Notebooks by pressing Shift + Enter and insert a new cell by clicking outside in the margin and typing a. In the next line, I can do print x and we see the value 5 printed out. Of course, in Jupyter Notebooks, we'll automatically display the value of the last line of each cell when you run it. So I can just type x and it displays that x. There are a few rules around variable names. They can't start with numbers, so I can do x1. Well, I haven't defined x1, but I definitely can't do 1x. And you see it's even highlighted in two different colors and I get a syntax error if I run that. And the syntax error occurs whenever Python can't understand what the heck you're trying to type. It doesn't understand a variable name that starts with a number. Variable names also can't have special characters except for an underscore. So you can also do uppercase or lowercase letters. Traditionally though, in Python, variable names always start with lowercase letters. Never start a variable name with an uppercase letter because it can get confused for something called a class, which we'll discuss later. But just remember, variable names start with lowercase letters. Now I've assigned things like 1, and 2, and 5 to these variables, and these are all numbers, obviously, but we could also do something like this, name = Ryan. And this sets the value of name to some text which programmers call a string. And the reason this is called a string has to do with the fact that it's a string of characters. So remember how computers lay things out in memory? Each character in the string gets its own segment of memory and the computer strings them together into these arbitrarily large strings. If you ever forget the name of a type of variable, there's a handy function for that. You can just use this Python function called type, give it the name of your variable, and it returns string, or str for string. We could also give it the x that we did, and we get int for integer. There are a lot of types of variables in Python. So let's just cover a few basic ones. Obviously, there are numbers. Programmers call whole numbers like 1, and 2, and 3 integers, or in python an int. But not all numbers are whole numbers. We also have decimals or floats. So 1.5, that's a float. Why are they called floats? Well, I can write a float like this. Most of the information is on the right-hand side of the decimal place. But I could also write a float like this, .9, most of the information is on the left of the number. Remember again how computers store things in memory. They also have to record where that decimal place is in the number, and they don't know whether all the information's going to be on the left or the right. So that decimal place kind of floats around. That's why it's called a float. Also, for any complex math applications, we can use complex numbers. Honestly, I've only used these a couple times in my career, but as one of the basic number types in Python, they're worth knowing about. So remember back from algebra, imaginary numbers are the square root of negative numbers and usually represented by the letter i. However, in Python, they use the notation common in engineering, which is j. So we can write, let's look at the type of 2j. It's a complex number. We can also do math with these. So 1j times 1j, that asterisk is multiplication, that gives us negative one plus 0j, or negative one. So remember i times i, or 1j times 1j, is negative one. I mentioned strings a little bit earlier, but let's go over some fun things to do with them. So strings are declared with single quotes, or with double quotes, you can also use double quotes. It's the popular style in Python these days. So that's what we'll be doing throughout this course. So I can also add strings together. So String 1 plus String 2 sticks the two strengths together, or as programmers say, concatenates the strings. And something interesting I can do is concatenate two strings that are numbers. So what's String 1 plus String 1? If you thought it was 2, you'd be very disappointed. It's actually 11, the string 11. Remember, strings are not numbers. So Python's just sticking these two strings together and that gives us the string 11. What happens if we do String 1 plus 1? So a string plus a number. (bomb booming) Just kidding, your computer's not going to explode. Python just prints out a type error, "can only concatenate string, not int, to string." So when you get errors in Python, it's important to read them and understand what's going on. The error will usually tell you what you're doing wrong. Finally, let's look at booleans. So booleans are true and false values. Note that they start with a capital letter here. If I try to do a lowercase true, it tries to interpret this as a variable name and says that it's not defined. However, if you want to be very confusing, you can write a little program like this. So how are booleans usually used in programming? Well, they're not usually used directly by typing true and false explicitly, but we usually get them as the result of statements like this one. Now remember a single equal sign, if we scroll up, this is an assignment operator. The double equal sign is a statement. It's saying 1 is equal to 1, and we get back true. We could also do 1 is equal to 2, which is, of course, false. This double equals is a comparison operator. So this is a comparison operator, we're comparing two values, versus the assignment operator, where we're assigning a value into a variable. So this is a lot of information from one video. Don't worry if you don't have everything memorized. You can't write a program without variables. And so we're going to be revisiting these concepts throughout the course.

Contents