Posted on

How to use variables in Python

Enroll in The Complete Python 3 Course: Beginner to Advanced!

Click here to subscribe for more videos like this!

Welcome back everyone, let’s talk about variables now. Now you can probably guess by the name a variable is something that has variable meaning that the name the value of it is not always going to be constant. So by explicitly stating strings they’re not reusable as well. If I wanted to you know say let’s jump into a Python console, alright. If I wanted to create a string not using variables, “This is a string” I could create that. However, there’s no way now for me to reuse it to do something else so I’d have to do “This is a string” + “Hello” and then there’s no way for me to reuse that one and see you can see the problem. Now that’s a one reason why we use variables is so we can reuse the variables, now another reason is because when you pass values into functions it’s going to take on the form of variable through the parameter list of the function, we will get more functions later. Now the difference between Python and some other programming languages is some programming languages are typed, and you might say well hey aren’t all programming languages typed? Yes, you type them with your fingers on the keyboard, however a typed language basically means that you need to define the type of variable. So as we learned in the previous videos the different types are integers, strings, Booleans and stuff like that. So if you’re setting a string variable you would have to explicitly tell the program that it’s a string but with Python you don’t need to do that. All you need to do is give your variable a name and a value. So, let’s go ahead and let’s create a variable called greeting and it’s going to equal a string that’s going to say “greeting = “hello world”” Now you’ll notice when I hit enter it didn’t print anything on the screen and it didn’t do anything noticeable and it’s because setting a variable doesn’t have any type of response, so it just creates variable and it’s there. Now we can use it by using greeting. So I could type “print(greeting)” and it’s going to print “hello world” Now also now that I’ve got it in a variable we can manipulate that variable in a number of ways. So I’m going to type “greeting = greeting.split(” “)[0]” So basically what we’re doing here, and I can’t use double quotation or double equal sign there, we’re setting greeting to a new value which is going to be greeting which we’re gonna split at the space, so we’re going to split it right here, and we’re going to get the first item in there. So it’s going to turn greeting into just Hello. So let’s hit enter here. We’ve assigned it a new value so if we now go ahead and “print(greeting)” you’re going to see that it says Hello. So now what we can do is now concatenate this with something else. So, we can do that in a number of ways we can reuse the previous example where we just let’s just print it out first, we can “print(greeting + “Nick”” and it’s going to say Hello Nick. Now if we wanted to store that in a variable we could do the same thing that we did before, or you can continue using this variable for different people. So I could say “print(greeting + “someone else”” and so that’s how that works. Now if i did want to make the greeting equal to concatenate a string all you have to do is redefine it as “greeting = greeting + “Nick” Now when we print out greeting you’ll see that it says Hello Nick. So that’s a variable and the useful things that you can do with it, now you can also assign values of other types. So we can say number is going to equal 1 and then second number is going to equal 2. So now we’ve got number and second number stored in variables. Let’s do something with that. So, what we’re just going to do is add these two numbers together in a print statement. So, “print(number * secondnumber + secondnumber * number)” and this is going to print out 4. So essentially what the print statement is doing is before it prints it out it’s calculating this because we’ve stored these as integers, and so it’s going 1*2+2*1 So it’s essentially saying 1*2 + 1*2 and it’s coming up with this, so. And as well you can set “check = True” So, that’s how to set variables, it’s pretty simple and we’re going to be doing a lot of this so this is a concept that you’re going to have to be very familiar with, and get used to using it. In the next video, we’re going to be talking about some built-in functions, and we’ve already actually talked about some of them.