That's enough of the interactive show for now.
Let's read our first program in Idle Click on file new file.
This makes the file editor window appear.
Enter the following code.
I'll explain what it does afterwards.
Pound.
This program says hello and asks for
my name.
I have a blank line of my print world.
Print, please.
Your name?
Pound.
Ask for their name.
My name equals input.
Print
this.
Good to meet you.
Plus my name
printed the length of your name.
Is print, Len.
My name.
Print.
What is your age?
Pounds. Simple.
Ask for their age.
My age equals input.
Print.
You will be
plus string in my age.
Plus one
plus in a year.
Saying this program as Hello Sky.
Python programs
or just text files that have the dot t y file extension.
Then click on Run
run manual or just press F5 to run the program.
Hello world.
What is your name? Al.
It's good to meet you, Al.
The length of your name is two.
What is your age? 30.
26. Yeah, I am 26.
You will be 27 in a year.
I remember when that was true.
Okay, That's the entire program.
One thing to keep in mind is that Python will start
at the first instruction at the top of the program
and then move downward, executing each instruction in turn.
Think of it as putting your finger on the top instruction
and as each instruction
is executed, moving your finger down to the next instruction.
This is just like how you might move your finger
along the lines of a book that you're reading.
Your finger is called The Execution.
The execution in Python starts at the top and moves down.
So the first
line with the pound sign is called a comment.
Python ignores comments.
You can use them to write notes to yourself or reminders about what
the code is trying to do or what the entire program does.
Blink lines are completely ignored by Python.
You can just use these to space out your code.
In fact, let's look at a couple more blank lines here, right here,
just to group together the code that's relevant
to asking for your name in the code that asks for your age.
The print function displays
the string value inside the parentheses on the screen for the line print.
Hello World means print out the text in the string.
Hello world.
Functions in python are like mini programs in your program.
They contain code that do various things and for your convenience, Python
comes with a lot of them.
In code. A function is its name,
followed by parentheses and optionally,
sometimes there are values passed to the function
inside the parentheses.
In this context, these values are called arguments,
but really values and arguments are the exact same thing.
This is called calling.
The print function.
When we have code like this here,
so the next instruction is also a call to the print function.
This time it's passing the What is your name?
String to the print function.
There's also a comment at the very end of the line
you can always add comments to the end of an instruction.
Since everything after that pound sign will just be ignored by Python
when the input function is called
Python waits for the user to type some text on the keyboard and press.
Enter.
Just like a variable will evaluate to a value, it contains calls to the input
function, evaluate to the string value of what the user typed.
So if I typed in l when input was called
this my name variable assignment statement,
we just evaluate two L
and then the string L
gets stored inside of my name.
Remember that expressions
can always evaluate down to a single value.
So if l is stored in my name,
then this is what that call to the print function looks like.
First my name evaluates to the value inside of it
and then this is just a string concatenation.
So the next step
looks just like this.
So this is the string that gets passed to the print function. Call
this next line introduces a new function called Len.
What Len does is it takes a string argument
and then evaluates to the integer value of the length of the string,
which is the number of characters in the string.
Let's just experiment with this function in the interactive shell for a little bit
so I can call the length function
and pass it out and it returns to
say, Albert
Fortran six or
length the blank string, and that returns zero.
And then I can just use these these function calls to Len anywhere.
I could use an integer so I could say,
you know, if I typed out my name
ten times, its length would be 20.
So then we have some more print and input calls.
This time we're saving whatever the user types into the variable my age.
And then we have this line which introduces the string and int functions.
Now this seems really complicated, so we'll just take this one step at a time.
The string and int functions return
string and integer values of whatever you pass them.
So this is really handy if you need to convert between data types.
So in
the interactive show you can have something like
string of value 26
and that returns a string value with 26 in it.
If you have to go the opposite route, can call the integer function and pass it
a string value like one, two, three, four,
and it will return an integer value.
There's also a float function that you can use
to convert something to a floating point value.
So if you can pass a string to it, it'll return a float value.
Or if you pass an integer value to it, it will return
a floating point version value of that integer.
Notice it has the decimal point end point zero.
This may seem pointless, but remember that the input function always returns
a string value, even if the string value is something like 26.
And we can't do math on a string
that returns an error.
First, we have to get an integer value of it,
so that's why we pass it to the
int function.
And just like you can't do addition on strings, you can't do string
concatenation on integers.
So we're going to have to call the string function,
convert that into a string value.
This is what the entire
last line looks like when it when Python evaluates it
first, the major variable
will evaluate to the value that's inside of it, and then that gets
passed to the int function, which returns an integer form of it.
We add one to that integer.
We have to convert that back to a string value
because we want to concatenate it with the other strings next to it.
And then after that is just the string concatenation.
And so all of this
just reduces down to this string which we pass to the print
function call, which then gets it displayed on the screen.
And that's how the entire program works.
Just to recap,
the file editor is where you type in code for a program.
The execution starts at the top and then proceeds down,
executing each instruction in turn.
Comments begin with a pound sign and are just notes that are ignored by Python.
They're just notes for the programmer.
Functions are kind of like mini programs that your program
can run to execute some code that does some specialized thing.
In this case, the print function has code
for making strings appear on the screen.
There's a print function for making text appear on the screen.
There's an input function for getting text from the keyboard,
and there's also a string int and a float function
that returns an integer string or a float version of the values that you pass them.