Python Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14|Last
Lessons:-INTRODUCTION

1.4. INTEGER ARITHMETIC 13
The Python Shell is an interactive interpreter. As you can see, after you press Enter, it is evaluating
the expression you typed in, and then printing the result automatically. This is a very handy environment
to check out simple Python syntax and get instant feedback. For more elaborate programs that you want
to save, we will switch to an Editor Window later.
1.4.2. Multiplication, Parentheses, and Precedence. Try in the Shell:
2 x 3
You should get your first syntax error. The ’x’ should have become highlighted, indicating the location where
the Python interpreter discovered that it cannot understand you: Python does not use x for multiplication
as you may have done in grade school. The x can be confused with the use of x as a variable (more on that
later). Instead the symbol for multiplication is an asterisk ’*’. Enter each of the following. You may include
spaces or not. The Python interpreter can figure out what you mean either way. Try in the Shell:
2*5
2 + 3 * 4
If you expected the last answer to be 20, think again: Python uses the normal precedence of arithmetic operations:
Multiplications and divisions are done before addition and subtraction, unless there are parentheses.
Try
(2+3)*4
2 * (4 - 1)
Now try the following in the Shell, exactly as written, followed by Enter, with no closing parenthesis:
5 * (2 + 3
Look carefully. There is no answer given at the left margin of the next line and no prompt >>> to start a
new expression. If you are using Idle, the cursor has gone to the next line and has only indented slightly.
Python is waiting for you to finish your expression. It is smart enough to know that opening parentheses
are always followed by the same number of closing parentheses. The cursor is on a continuation line. Type
just the matching close-parenthesis and Enter,
)
and you should finally see the expression evaluated. (In some versions of the Python interpreter, the interpreter
puts ’...’ at the beginning of a continuation line, rather than just indenting.)
Negation also works. Try in the Shell:
-(2 + 3)
1.4.3. Division and Remainders. If you think about it, you learned several ways to do division.
Eventually you learned how to do division resulting is a decimal. Try in the Shell:
5/2
14/4
As you saw in the previous section, numbers with decimal points in them are of type float in Python. They
are discussed more in Section 1.14.1.
In the earliest grades you would say “14 divided by 4 is 3 with a remainder of 2”. The problem here is
that the answer is in two parts, the integer quotient 3 and the remainder 2, and neither of these results is the
same as the decimal result. Python has separate operations to generate each part. Python uses the doubled
division symbol // for the operation that produces just the integer quotient, and introduces the symbol %
for the operation of finding the remainder. Try each in the Shell
14/4
14//4
14%4
Now predict and then try each of
23//5
23%5
20%5
6//8
6%8

 
 
 
image
Anil  Bist

Skills    Python

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)