0% found this document useful (0 votes)
19 views5 pages

Class 5 (BSD)

This document provides a tutorial on creating a simple Python program using the Mu file editor, including instructions for writing code, saving files, and running programs. It explains the use of functions like print(), input(), and len(), as well as the importance of comments in code. The tutorial emphasizes the process of user interaction through input and output, and the conversion of data types using functions like str() and int().

Uploaded by

uzairznkhattak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Class 5 (BSD)

This document provides a tutorial on creating a simple Python program using the Mu file editor, including instructions for writing code, saving files, and running programs. It explains the use of functions like print(), input(), and len(), as well as the importance of comments in code. The tutorial emphasizes the process of user interaction through input and output, and the conversion of data types using functions like str() and int().

Uploaded by

uzairznkhattak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Class 5 ( Our First Program )

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:

print('Hello World') # print function that prints stuff on your


screen)
print('What is your Name') # asks for your name ( but only prints)
doesnt give you a slot to write
myName = input() # creates a variable named myName and
makes it empty by giving an input() functions so user can write something
or his name
print(‘it is good to meet you’ + myName)
print('The number of letters in your name are') #prints this on the
screen
print(len(myName)) #len() function is used to return an
integer value that indicates the number of items in the object, so here the
number of items in the variable myName
print('What is your age') # asks your age
myAge = input() # created the variable myAge with the input
so the user can write in it his age.
print('You will be ' +str(int(myAge)+1) + ' in next year. ') # we added the
two different data types, by converting the integer myAge in string
Once you’ve entered your source code, save it to so that you wont have to retype it
each time you start Mu, Click the save button, enter hello.py in the File Name field, and
then click Save.
You should save your programs every once in a while as you type them. That way, if the
computer crashes or you accidentally exit Mu, you wont lose the code. As a shortcut,
you can press Ctrl-S on windows to save your file.
Once you’ve saved, let’s run our program. Press the F5 key. Your program should run
in the interactive shell window.
Enter your name when your program asks for it. The program’s output in the interactive
shell should look something like this:

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.

Dissecting Your Program


With your new program open in the file editor open in the file editor, Lets take a
quick tour of the python instructions it uses by looking each line of code does.
Comments
The Following line is called a comment.

# print function that prints stuff on your screen)

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 Print() Function.


The print() function displays the string value inside its parentheses on the screen.

print(‘ Hello World!’)


print(‘What is your name?’) # asks for their name

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.

The Input() Function


The input() function waits for the user to type some text on the keyboard and press
ENTER.

➔ Variable name myName = input() <- string

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.

Printing the User’s Name


The following call to print() actually contains the expression ‘ It is good to meet you,’ +
myName between the parentheses.

Print(‘It is good to meet you,’ + myName)

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:


You can pass the len() Function a string value ( or variable containing a string), and the
function evaluates to the integer value of the number of the character in that string.

print(‘The length of your name is:’)


print(‘len(myName))

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.

You might also like