Tip:
Highlight text to annotate it
X
In this video we are going to learn about doing simple input output in Python and doing
simple variables. This is related to module 2. So we’re going to start a new window.
Like we talked in an earlier video, that you can only save files that you write as a script
not interactive. SO to declare a variable in Python unlike other programming languages
you don’t need to give a data type a datatype basically defines what kind of data a variable
can store and what operations can be performed in that variable.Python variables automatically
pick up their types when you give them a first assignment. so if i wanted a variable for
example ’name’ I just type in name and say equal to Sammy Snake ok now name is a
declared memory location now the equal to sign means that whatever is in the right side
of that equal to sign needs to be assigned to the memory location that is on the left
side. Because what we have on the right side is enclosed in double quotes that is a string.
It’s counted as a string. SO now if we wanted to print this out we would say print name
that’s a simple print and if I run this module, I have to save it first before it
lets me run it, I am going to save it to….and this is the result that I get because I said
print name. Ok so I am going to go back to that window and I am going to do some editing
, I don’t just want to print the name I want to say ‘my name is’ so now we need
to add a label, to add a label I start a double quote and I say “my name is” put a space
after the is and put a comma. A comma is a separator, its separates two different kinds
of variables from each other. Even though these are strings if I said my name is name,
and I put name inside the quotes, lets see what that does to our program. We can run,
run module. My name is name, that’s not what I want. I want it to say my name is Sammy
Snake. Ok so I get rid of the name that’s in the quotes whatever is in the quotes is
gonna be printed exactly as it is and I put name outside the quotes. What that does is
it tells the Python interpreter print this string exactly as it is and now from the variable
name pick up its value and print the string that along with the name next to the string.
If I wanted name printed on the next line, then I would have to add an end of line character
here. Which is an escape sequence, you can write write many different kinds of escape
sequences you precede them with a back slash, and if I say backsplash n, its going to put
a new line, if I say backslash t thats going to put a tab character and so on. Ok so this
is going to print my name is Sammy Snake because we got the name outside the quotes. I going
to go back to this window again and I am going to say I going to have another variable there,
age = 21, Now I want to print out age, so I say print age. SO lets look at the result
of this it says My name is Sammy Snake and 21 is the next line so if I wanted to add
a label to the age I would do it the same way I would say and I am age, and put a comma
after age and say years old. Now lets see what this does what I have printed is three
different things; I’ve printed a string, I have printed a variable that holds a numeric
which is 21, 21 is not enclosed in quotes so it is numeric and then I have printed another
string after that and what are they separated with, they are separated with commas. So I
say run, run module it says my name is Sammy Snake and I am 21 years old. What if I wanted
to combine these two within the same print statement, well then I just put a comma after
name and I bring everything up and I am going to change this to a, and I am 21 years old
let’s run that and see how that goes. My name is Sammy Snake and I am 21 years old,
that comes on the same line. Now what if I wanted Sammy Snake to calculate the age in
human years? We’ll get to calculations in a future video but let’s look at this, what
if we wanted to reassign the value in age? Age is equal to age +7 that’s a calculation
or I could say age is equal to 65, now print I will retire when I am and I say age. Now
age has a value of 21 now it has 65 lets see what I get. It says my name is Sammy Snake
and I am 21 years old. I will retire when I am 65 variable age is a simple variable
it can only hold one value at a time so once we assigned 65 to age 21 was deleted and the
new value of age was picked up. So this is how you print together multiple things in
the same print statement. And in the next video I will show you how to do input.