declare

Python Programming #2 - Variables and Multiple Assignment



Sharing buttons:

all right I'll be here and welcome to

part two and getting started with Python

now we left off where we gonna print

statement down we get PyCharm Python

both installed now it's actually time to

start writing Python so the first thing

we're going to talk about is variables

all right what are variables how do we

initialize them store values all that

good stuff so what is a variable well

hopefully in any other programming

languages you might have dealt with you

know what variables are variables are

basically memory locations to store

values if my friend is turning 20 years

old all right I want to store that value

20 in a variable I can do that let's say

I'm stating that my name is avi I can

store that sentence that's string in a

variable all variables are there they're

just data they're just memory locations

to store values so how do we do that in

Python well let's take a look so instead

of writing in this file where we'll

normally write code just for simplicity

we're going to be using the Python

console now if you don't see the Python

cons console like this down below what I

want you to do is hover over this sort

of menu icon in the bottom left of your

screen and you'll see a menu with a list

of different things you can open and we

want to open the Python console so

immediately you can see the Python

console has open now yours might be a

bit below just go ahead and drag it up

so to initialize a variable in Python

there's three steps the first step is

the variable name in other languages if

the right bar in it all the random stuff

before the variable name there's none of

that it's just the variable name so

we're talking about my friend he's

turning 20 we're gonna write H and I

like lowercase so H okay the second step

is an equal sign you have to assign your

variable to a value and you do that

through an equal sign so H equals and

then finally you write the value in this

case he turned 20 so we're gonna say age

equals 20 hit enter and now age the

variable has been assigned

value 20 if we type age in the console

we get the value 20 fantastic now let's

go back to the sentence my name is Augie

I want to store that sentence in a

variable so I'm going to create a

variable called sentence hit equals and

in quotation marks because it's a string

I'm going to say my name is avi go ahead

and enter and now that's string that

sentence has been assigned to the

variable sentence so now if I want to

take a look at it again or view it view

the data inside the variable just type

it hit enter and you get my name is avi

fantastic that's all there is to

variables so you assign data you can

print it you can use it in various

methods we'll cover that in later

lectures but all I wanted you to

understand is how to create these

variables now Python also has a new

feature known as multiple assignment

suppose I have a bunch of friends who

spread this today

once turning 16 and others turning 18

three more turning 21 doesn't matter um

based on what we know right now we'd say

oh let's say Saurez age she's turning 16

Bob's age he's turning 21

Michael Mikey's turning 17 this is

tedious right we have to create a new

variable on me every line but with

multiple assignment in one line you can

assign the values to all the variables

the way that works is very simple I

wanna sign Sara Bob and Mike the age is

16 21 and 17 respectively what I say is

Sara comma Bob comma Mike is equal to 16

comma 21 comma 17 all right

in this case sara is getting the value

of 16 Bob is getting the value of 21

Mike is getting the value of 17 hit

enter and now if I want say Bob's value

I get 21 if I want Mike's value I get 17

that's the way multiple assignment works

instead of having to do it on every

individual line you can get all your

variables populated in one single line

now suppose you had 10 friends who are

turning 17

today instead of having to do this ten

times one variable for each person and

then seventeen ten times you can assign

each variable the same value by saying

Sarah is equal to Bob is equal to Mike

is equal to seventeen by doing so each

one of these variables has been assigned

the same value 17 so now if I say Sarah

I get 17 Bob is 17 and Mike is 70 that's

the power of multiple assignment to make

your Python code swifter and easier

right you don't have to go through the

process of multiple Decker

declaration in every line it's all in

one light nice and easy so for a quick

challenge let's go ahead and try

something out I want you to create in

one line two variables one should be

your age and one should be your name the

first one should be a normal number like

we've discussed and the other one should

be a sentence

so using this multiple assignment go

ahead and give it a try your name and

your age all right hopefully you're able

to do it if you weren't don't worry

we're just getting started so I'm going

to go ahead and say name comma H is

equal to now the first one since it is a

string I have to put it in quotation

marks and I'm going to say avi and then

I'm gonna say comma my age 15 awesome so

now let's go ahead and check the values

name and H great so that's it for this

lecture I'm a quick recap we learn what

variables are we learned how to

initialize them there are three parts

you have the variable name and equal

sign and the value and then we learned

about multiple assignment and declaring

multiple variables in one line don't

forget to Like share and subscribe and

I'll see you in the next video