Python Calculator
Let’s try some simple Python commands. Start the interpreter and wait for the primary prompt,>>>
. (It shouldn’t take long.)Python Calculator |
Numbers
The interpreter works as a simple calculator (Python Calculator): you can type an expression at it and it will print the value. Expression syntax is straightforward: the operators +, -, * and / work similar to in most alternative languages (for example, Pascal or C); parentheses (()) may be used for grouping. For example:
Integer numbers (such as 2, 4, 20) have type int, float in a numerator (eg 5.0, 1.6).
We will see more about numerical types later in the tutorial. Division (/) always returns a float. To split the floor and get integer results
(Discarding any partial result) You can use the // operator; You can use% to calculate the remainder:
It is possible to use the ** operator to calculate powers 1 with Python :An equal sign (=) is used to assign a value to a variable. After this, no result is
displayed before the next interactive prompt:If a variable is "not defined" (a value specified), then trying to use it will give you
an error:There is full support for floating-point; Operators with mixed type operands convert
integer operators to floating points:In interactive mode, the last printed expression variable is assigned to _. This means that when you are using Python calculator, it is slightly easier to
continue calculations, for example:
This variable should be treated as read-only. Do not explicitly specify a value for this -
you will create an independent local variable that will have the same name masking the
built-in variable with your magical behavior.Strings
In addition to numbers, Python can also manipulate strings, which can be expressed in
many ways. They can be enclosed in a single quote ('...') or Double Result ("...") with the same result. 2. \ _ can be used to avoid citations:
In the interactive interpreter, the output string is enclosed in quotes and special
characters are escaped with a backslash. Although it can sometimes look different from
the input (the enclosed quotation may change), the two strings are equivalent. If the
string contains a single quote and there are no double quotes, the string is enclosed
in double quotes, otherwise, it is enclosed in single quotes. The print () function produces a more readable output, except for the enclosed citations
and saved and special print Characters:If you do not want characters preceded to be \ interpreted as special characters, you can
use the raw string by adding r before the first quotation:String literals can span multiple lines. One method is using triple-quotes: "" "..." ""
or '"' ... '". The end of the rows is automatically included in the string, but it is possible to stop
it by adding a \ "at the end of the row. The following example:
Strings can be combined with the + operator (glued together), and repeated with *:Two or more string literals (ie enclosed between blocks) are automatically leveled next toeach other.This feature is particularly useful when you want to break a long strings:This only works with two literals though, not with variables or expressions:If you want to concatenate variables or a variable and a literal, use+
:Strings can be indexed, with the first character index being 0. There is no separatecharacter type; A character is simply a string of one size:Indices may also be negative numbers, to start counting from the right:Note that since -0 is the same as 0, negative indices start from -1. In addition to sequencing, slicing is also supported. Indexing is used to obtain individualcharacters, slicing allows you to get substrings:Note how the beginning is always included, and the end is always excluded. This ensuresthat s [: i] + s [i:] is always equal to s:Slice indices have useful defaults; A omitted first index defaults to zero, an augmentedsecond index default is truncated to the size of the string.One way to remember how the slice works is to think about pointing the indices betweencharacters, with the first character's left edge being 0.. Then the right edge of the lastcharacter of a string of n characters is the index n, for example:The first line of numbers gives the position of the indices 0… 6 in the string; The secondline indicates the same negative. Slices from i to j contain all the characters betweenlabeled edges i and j, respectively.
Python Calculator
Reviewed by Sk Jha
on
October 08, 2019
Rating:
No comments: