Python Logbook: Submitted To
Python Logbook: Submitted To
Lab#1
Submitted to:
Dr. Ali Tahir
1.2 Use python prompt to calculate the area of a circle of radius 1.5
using the formula πr2.
pi=3.142
r=1.5
a=pi*r*r
print "Area of circle is=" a
Output: 7.0695
Result
When import the circle.py file. Its show the area of circle directly
which is below.
Result
Question:
What does the .1 between the first % and f do in the above expressions? Try these
using .2 and .3 instead of .1 in Python to confirm whether or not you got the correct
answer?
Answer:
3+4-2+5
In Python, the left operand is always assessed earlier the right. In this is the correct
answer .c is not possible. both plusses first, then the minus.
Question a:
In which of the following order(s) was it possible for the 2 plusses and 1 minus to be
evaluated to get 10? In which was it not possible to get ten?
Answer:
What effect did the round () brackets have on the order of evaluation?
Answer:
The value first executes which will in round () brackets. And other after this
Question: c.
How might you use brackets to get the same result you would get without brackets?
Answer:
(8-5)-3=0
Results
Answer:
Answer:
Questions: c. If you asked someone who had learned about division, but not about fractions
yet, "what is 7 divided by 3?" which result would they give for the dividend and the
remainder?
Answer:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
S P A M A N D E G G G S
Result of [5:8]
S P A M A N D E G G G S
S P A M A N D E G G G S
S P A M A N D E G G G S
Len=13
Result
a. Describe in your own words how the slicing operator [start:end] applies to
extracting parts of the string object called: meal where start and end are starting and
ending indexes for the slice.
Answer:
In the slicing operator get the integer also indexes from start and takes it upto end of the
string and gives output.
b. What effect does leaving out start or end have on the slice?
Answer:
In the condition of leaving out start or end it takes zero as default value of start
i.e. [:3] = [0:3] and in other case leaving out end it get the last index as end by default
[5:] = [5:0]
c. How would you specify a slice of meal that prints 'egg'? How would you specify a slice of
meal that prints 'egg'?
Answer:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
S P A M A N D E G G G S
Answer:
0 1 2 3 4 5 6 7 8 9 10 11 12 13
S P A M A N D E G G G S
e. What do you think the len() function does? Use the Python interpreter to check whether
your answer is right, using len() on other string objects e.g. len("123")
Answer:
The len function give the value and tell us about total number of characters in
strings. len(“123”)=3
f. Run Python to assign your full name (e.g. "Josiah Haddock Cheese") to a string object
called name, and use a slice operator to output just your middle name. If you have not got a
middle name, make one up for the purpose of the exercise. See if it is possible on your
system to cut and paste the lines of Python code you used to do this job into a text file. In
either case save a text file which you can run as a Python program "midname.py" which
extracts and prints your middle name.
Answer:
Answer:
In this case take the string start and index from end. Due to negative value string work
reversely.
2nd
Question a: How would you print out the number of spoonfuls to exactly 2
decimal places?
Answer:
Result
Explain in your own words the difference between %f, %d and %s when substituting
different types of variable object within strings. Use the Python interpreter to check what
happens when you use the wrong letter after % for the object you want to substitute and
write what happens in your logbook.
Answer:
The %f is use to format float and show value in Decimal 2.000 but its depend what is
the value mention with %f i.e. %.1f %.5f etc.
Result
%d
I put the value in fraction form but output in integer.
Result
Question c:
Why were 2 % symbols needed after .1f to print out a single % symbol?
Answer:
In the above condition first % Modulus symbol give the command print next
value same and as it is and the second % Modulus symbol tells to print
percentage % symbol.
Output: