Вы находитесь на странице: 1из 19

PYTHON LOGBOOK

Lab#1

Submitted to:
Dr. Ali Tahir

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


Lab 1

1.1 Python Version Information


When we open the Python IDLE Python information show above prompt
which is below mention.
Python 2.7.10 (default, May 23 2015, 09:40:32) [MSC v.1500 32 bit (Intel)]
on win32

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

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


1.3 What happen when you import the circle program.

When import the circle.py file. Its show the area of circle directly
which is below.
Result

1.4 Adding and Subtracting in Python


Results

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


1.5 Printing out values with a specified precision

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:

When we change it from .1 to .2 and .3 it returns, then 1 digit after decimal if .2


the 2 digits after decimal as well as show the value which we require.

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


1.6 Evaluation order and precedence of expressions in Python.

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:

The answer is option a. all + and - operators from left to right

a. all + and - operators from left to right =10


b. all operators from right to left =10
c. both plusses first, then the minus
d. minus first, then the plusses =10

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


Question: b.

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

1.7 Multiplying and dividing using Python

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


Question a: What does the * operator do with the operands 3 and 5?

Answer:

The * operator multiply the both values. Use for Multiplications

Question b: Which operation was carried out first, multiplication or addition?

Answer:

Multiplication operation run first according to DMAS rule.

Rule: D: David, M: Multiplications, A: Additions S: Subtracts

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:

That would give 1 remainder and 2 as dividend.

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


1.8 Using Python with strings: slices and length
Answer:
Meal= “spam and eggs”

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]

In [5:8] condition string shows the value 5:8 which “and”

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


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

[3:] in this condition output up to last value.


Result

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


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

Length calculation meal


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

Len=13

Result

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


Questions:

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:

print meal [9:12]

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

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


d. What index value does start use for the first letter (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:

name=” saif ullah Bhutta”

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


g. Experiment with the slice [:-1] applied to your full name and write down what happens.
Try again with [:-2] . Try this operation on a string of different length. Write down your
conclusions.

Answer:

In this case take the string start and index from end. Due to negative value string work
reversely.

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


1.9 Printing variables within strings
1st

2nd

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


3rd

Question a: How would you print out the number of spoonfuls to exactly 2
decimal places?

Answer:

"I like %.2f spoonfuls of sugar in my coffee" % (3/2.0)

Result

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


Question b:

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.

%d In this condition, %d is use to show value in Integers

%s It is use for string

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


%s

Result

%d
I put the value in fraction form but output in integer.

Result

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


%F

Question c:

Why were 2 % symbols needed after .1f to print out a single % symbol?

"The price includes VAT of %.1f%%" % VAT

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:

Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah


Saif.zaib@gmail.com Programming and Geodatabase Submitted by: Saifullah

Вам также может понравиться