Class 5 (BSD)
Class 5 (BSD)
So, Though, the interactive shell is good for running python instructions one at a time, to
write Entire Python Programs, You’ll have to type the instructions into the file editor. The
File Editor is similar to text editors such as Notepad, but it has some features
specifically for entering source code. To open a new file in Mu, Click New Button on the
top row.
The window that appears should contain a cursor awaiting your input, but its different
from the interactive shell, which runs python instructions as soon as you just press
ENTER.
Your file editor lets you type in many instructions as you want, save the file, and run the
program.
Here’s how you can tell the difference between the two:
• The interactive shell window will always be the one with the >>> prompt.
• The File editor window will not have the >>> prompt.
Now its time to create your first program! When the file editor window opens, Enter The
following into it:
When there are no more lines to execute, the python program terminates; that is, it
stops running. ( you can also says that python program exits.)
You can close the file editor file editor by clicking the X at top of the window.
To reload a saved program, select Load > Open from the menu. Do that now, and in the
window that appears, choose hello.py and click the Open button.
Your previously saved hello.py should be opened in the file editor window.
Python ignores comments, and you can use them to write notes or remind yourself what
the code is trying to do. Any text for the rest of the line following a hash mark (#) is part
of comment.
Sometimes, Programmers will put a # infront of a line of code to temporarily remove it
while testing a program, This is called commenting out code, and it can be useful when
you are trying to figure out why a program isn’t working. You can remove the # later when
you are ready to put the line back in.
Python also ignores the blank line after the comment. You can add as many blank lines
to your program as you want. This can make your code easier to read, Like paragraphs in
a book.
The line print(‘Hello world!’) means “ Print out the text in the string ‘ Hello world!’. When
python executes this line, you can say that python is calling the print() function and
string value is being passed to the function. A value is passed to a function call is an
argument. Notice that the quotes are not printed on the screen, They just mark where
the string begins and ends; They are not part of the string value.
You can also use this function to put a blank line on the screen; just call print() with
nothing in between the parentheses.
When you write a function name, The opening and closing parentheses at the end
identify it as the name of the function. WE WILL STUDY ABOUT FUNCTIONS THIS IN
LATER CLASSES IN DETAILS.
This function call evaluates to a string equal to the user’s text, and the line of code
assigns the myName variable to this string value.
Its like this function will tell the computer to ask you the value that will be equal to the
string, and the string will be assigned to the variable that is created with the variable
myName.
You can think of the input() function call as an expression that evaluates to whatever
string the user typed in. If the user entered ‘ Uzair ‘ then the expression would evaluate
to myName = ‘Uzair’
If you call input() and see an error message, like NameError: name ‘ Uzair’ is not defined,
you might be using an old version of python.
Remember that expressions can always evaluate to a single value, if ‘ Uzair’ is a value
stored in myName on the line 3, then this expression evaluates to ‘ It is good to meet
you, Uzair’. This single string value is then passed to print(),
The len() function is used to return an integer value which indicates the number of items
in an object.
Enter the following into the interactive shell to try this:
>>> len(‘Hello’)
5
>>> len(‘My very energetic monster just scarfed nachos.)
46
>>> len(‘’)
0
Just like those examples, len(myName evaluates to an integer. It is then passed to print()
to be displayed on the screen. The print() function allows you to pass it either integer
values or string values.
The Str(), int(), and float() Functions.
If you want to concatenate an integer such as 29 with a string to pass to print(), you’ll
need to get the value ’29’ which is the string form 29. The Str() function can be passed
an integer value and will evaluate to a string value version of the integer, as follows:
( in python programming, you can say evaluation means just working on what the code
says)
>>>str(29)
‘29’
>>>print(‘I am’ + str(29) + ‘years old.’)
I am 29 years old.