programming like c, c++, java,python learn in free

Monday, April 20, 2020

Number in python

If you’ve done any programming or scripting in the past, some of the object types in
Table 4-1 will probably seem familiar. Even if you haven’t, numbers are fairly straight-
forward. Python’s core objects set includes the usual suspects: integers that have no
fractional part, floating-point numbers that do, and more exotic types—complex 
numbers with imaginary parts, decimals with fixed precision, rationals with numerator and
denominator, and full-featured sets. Built-in numbers are enough to represent most
numeric quantities—from your age to your bank balance—but more types are available
as third-party add-ons.
Although it offers some fancier options, Python’s basic number types are, well, basic.
Numbers in Python support the normal mathematical operations. For instance, the
plus sign ( + ) performs addition, a star ( * ) is used for multiplication, and two stars ( ** )
are used for exponentiation:
>>> 123 + 222
# Integer addition
345
>>> 1.5 * 4
# Floating-point multiplication
6.0
>>> 2 ** 100
# 2 to the power 100, again
1267650600228229401496703205376
Notice the last result here: Python 3.X’s integer type automatically provides extra 
precision for large numbers like this when needed (in 2.X, a separate long integer type
handles numbers too large for the normal integer type in similar ways). You can, for
instance, compute 2 to the power 1,000,000 as an integer in Python, but you probably
shouldn’t try to print the result—with more than 300,000 digits, you may be waiting
awhile!
>>> len(str(2 ** 1000000))
301030
# How many digits in a really BIG number?
This nested-call form works from inside out—first converting the ** result’s number
to a string of digits with the built-in str function, and then getting the length of the
resulting string with len . The end result is the number of digits. str and len work on
many object types; more on both as we move along.
On Pythons prior to 2.7 and 3.1, once you start experimenting with floating-point
numbers, you’re likely to stumble across something that may look a bit odd at first
glance:
# repr: as code (Pythons < 2.7 and 3.1)
>>> 3.1415 * 2
6.2830000000000004
>>> print(3.1415 * 2)
6.283
# str: user-friendly
The first result isn’t a bug; it’s a display issue. It turns out that there are two ways to
print every object in Python—with full precision (as in the first result shown here), and
in a user-friendly form (as in the second). Formally, the first form is known as an object’s
as-code repr , and the second is its user-friendly str . In older Pythons, the floating-point
repr sometimes displays more precision than you might expect. The difference can also
matter when we step up to using classes. For now, if something looks odd, try showing
it with a print built-in function call statement.
Better yet, upgrade to Python 2.7 and the latest 3.X, where floating-point numbers
display themselves more intelligently, usually with fewer extraneous digits—since this
book is based on Pythons 2.7 and 3.3, this is the display form I’ll be showing throughout
this book for floating-point numbers:
# repr: as code (Pythons >= 2.7 and 3.1)
>>> 3.1415 * 2
6.283
Besides expressions, there are a handful of useful numeric modules that ship with
Python—modules are just packages of additional tools that we import to use:
>>> import math
>>> math.pi
3.141592653589793
>>> math.sqrt(85)
9.219544457292887
The math module contains more advanced numeric tools as functions, while the ran
dom module performs random-number generation and random selections (here, from a
Python list coded in square brackets—an ordered collection of other objects to be in-
troduced later in this chapter):
>>> import random
>>> random.random()
0.7082048489415967
>>> random.choice([1, 2, 3, 4])
1
Share:

0 comments:

Post a Comment

Unordered List

recentposts1
Powered by Blogger.

Text Widget

Featured Post

C programming Question and Answer Day-15

71. Program to check Niven number (Harshad number). #include<stdio.h> #include<conio.h> void main() {     int n, d, a, su...

Search This Blog

recent comments

recentcomments

Labels

[slideshow][technology]

vertical posts

[verticalposts][technology]

business

[business][grids]

ad space

ads 600

vehicles

[cars][stack]
[verticalposts][food]

our facebook page

about us

logo

Jupiter is a magazine responsive Blogger template. It has everything you need to make your blog stand out. This template is fully customizable and very flexible and we believe you will love it as much as we do.

Slide show

[people][slideshow]

health

[health][btop]

recent posts

recentposts1

Subscribe Us

random posts

randomposts2

Blog Archive

Recent Posts

top ads

Unordered List

recent

Pages

Theme Support

[socialcounter] [facebook][#][215K] [twitter][#][115K] [youtube][#][215,635] [rss][#][23M] [linkedin][#][21.5K] [instagram][#][600,300]