Function & Description
1
|
int(x [,base])
Converts x to an integer. base specifies the base if x is a string.
|
2
|
long(x [,base] )
Converts x to a long integer. base specifies the base if x is a string.
|
3
|
float(x)
Converts x to a floating-point number.
|
4
|
complex(real [,imag])
Creates a complex number.
|
5
|
str(x)
Converts object x to a string representation.
|
6
|
repr(x)
Converts object x to an expression string.
|
7
|
eval(str)
Evaluates a string and returns an object.
|
8
|
tuple(s)
Converts s to a tuple.
|
9
|
list(s)
Converts s to a list.
|
10
|
set(s)
Converts s to a set.
|
11
|
dict(d)
Creates a dictionary. d must be a sequence of (key,value) tuples.
|
12
|
frozenset(s)
Converts s to a frozen set.
|
13
|
chr(x)
Converts an integer to a character.
|
14
|
unichr(x)
Converts an integer to a Unicode character.
|
15
|
ord(x)
Converts a single character to its integer value.
|
16
|
hex(x)
Converts an integer to a hexadecimal string.
|
17
|
oct(x)
Converts an integer to an octal string.
|
Python - Basic Operators
Operators are the constructs which can manipulate the value of operands.
Consider the expression 4 + 5 = 9. Here, 4 and 5 are called operands and + is called operator.
Types of Operator
Python language supports the following types of operators.
Arithmetic Operators
Comparison (Relational) Operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Let us have a look on all operators one by one.
Python Arithmetic Operators
Sr.No.
|
Operator & Description
|
1
|
**
Exponentiation (raise to the power)
|
2
|
~ + -
Complement, unary plus and minus (method names for the last two are +@ and -@)
|
3
|
* / % //
Multiply, divide, modulo and floor division
|
4
|
+ -
Addition and subtraction
|
5
|
>> <<
Right and left bitwise shift
|
6
|
&
Bitwise 'AND'
|
7
|
^ |
Bitwise exclusive `OR' and regular `OR'
|
8
|
<= < > >=
Comparison operators
|
9
|
<> == !=
Equality operators
|
10
|
= %= /= //= -= += *= **=
Assignment operators
|
11
|
is is not
Identity operators
|
12
|
in not in
Membership operators
|
13
|
not or and
Logical operators
|
Python - Decision Making
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
Following is the general form of a typical decision making structure found in most of the programming languages −
Python programming language assumes any non-zero and non-null values as TRUE, and if it is either zero or null, then it is assumed as FALSE value.
Python programming language provides following types of decision making statements. Click the following links to check their detail.
Sr.No.
|
Statement & Description
|
1
|
if statements
An if statement consists of a boolean expression followed by one or more statements.
|
2
|
if...else statements
An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.
|
3
|
nested if statements
You can use one if or else if statement inside another if or else if statement(s).
|
Python – Loops
In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times.
Programming languages provide various control structures that allow for more complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times. The following diagram illustrates a loop statement −
Python programming language provides following types of loops to handle looping requirements.
Sr.No.
|
Loop Type & Description
|
1
|
while loop
Repeats a statement or group of statements while a given condition is TRUE. It tests the condition before executing the loop body.
|
2
|
for loop
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
|
3
|
nested loops
You can use one or more loop inside any another while, for or do..while loop.
| Loop Control Statements
Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
Python supports the following control statements. Click the following links to check their detail.
Let us go through the loop control statements briefly
Sr.No.
|
Control Statement & Description
|
1
|
break statement
Terminates the loop statement and transfers execution to the statement immediately following the loop.
|
2
|
continue statement
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
|
3
|
pass statement
The pass statement in Python is used when a statement is required syntactically but you do not want any command or code to execute.
|
Python - Numbers
Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object.
Number objects are created when you assign a value to them. For example −
var1 = 1
var2 = 10
You can also delete the reference to a number object by using the del statement. The syntax of the del statement is −
del var1[,var2[,var3[....,varN]]]]
You can delete a single object or multiple objects by using the del statement. For example −
del var
del var_a, var_b
Python supports four different numerical types −
int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.
long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.
float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).
complex (complex numbers) − are of the form a + bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.
Examples
Here are some examples of numbers
int
|
long
|
float
|
complex
|
10
|
51924361L
|
0.0
|
3.14j
|
100
|
-0x19323L
|
15.20
|
45.j
|
-786
|
0122L
|
-21.9
|
9.322e-36j
|
080
|
0xDEFABCECBDAECBFBAEL
|
32.3+e18
|
.876j
|
-0490
|
535633629843L
|
-90.
|
-.6545+0J
|
-0x260
|
-052318172735L
|
-32.54e100
|
3e+26J
|
0x69
|
-4721885298529L
|
70.2-E12
|
4.53e-7j
|
Python allows you to use a lowercase L with long, but it is recommended that you use only an uppercase L to avoid confusion with the number 1. Python displays long integers with an uppercase L.
A complex number consists of an ordered pair of real floating point numbers denoted by a + bj, where a is the real part and b is the imaginary part of the complex number.
Number Type Conversion
Python converts numbers internally in an expression containing mixed types to a common type for evaluation. But sometimes, you need to coerce a number explicitly from one type to another to satisfy the requirements of an operator or function parameter.
Type int(x) to convert x to a plain integer.
Type long(x) to convert x to a long integer.
Type float(x) to convert x to a floating-point number.
Type complex(x) to convert x to a complex number with real part x and imaginary part zero.
Type complex(x, y) to convert x and y to a complex number with real part x and imaginary part y. x and y are numeric expressions
Mathematical Functions
Python includes following functions that perform mathematical calculations.
Sr.No.
|
Function & Returns ( description )
|
1
|
abs(x)
The absolute value of x: the (positive) distance between x and zero.
|
2
|
ceil(x)
The ceiling of x: the smallest integer not less than x
|
3
|
cmp(x, y)
-1 if x < y, 0 if x == y, or 1 if x > y
|
4
|
exp(x)
The exponential of x: ex
|
5
|
fabs(x)
The absolute value of x.
|
6
|
floor(x)
The floor of x: the largest integer not greater than x
|
7
|
log(x)
The natural logarithm of x, for x> 0
|
8
|
log10(x)
The base-10 logarithm of x for x> 0.
|
9
|
max(x1, x2,...)
The largest of its arguments: the value closest to positive infinity
|
10
|
min(x1, x2,...)
The smallest of its arguments: the value closest to negative infinity
|
11
|
modf(x)
The fractional and integer parts of x in a two-item tuple. Both parts have the same sign as x. The integer part is returned as a float.
|
12
|
pow(x, y)
The value of x**y.
|
13
|
round(x [,n])
x rounded to n digits from the decimal point. Python rounds away from zero as a tie-breaker: round(0.5) is 1.0 and round(-0.5) is -1.0.
|
14
|
sqrt(x)
The square root of x for x > 0
| Random Number Functions
Random numbers are used for games, simulations, testing, security, and privacy applications. Python includes following functions that are commonly used.
Sr.No.
|
Function & Description
|
1
|
choice(seq)
A random item from a list, tuple, or string.
|
2
|
randrange ([start,] stop [,step])
A randomly selected element from range(start, stop, step)
|
3
|
random()
A random float r, such that 0 is less than or equal to r and r is less than 1
|
4
|
seed([x])
Sets the integer starting value used in generating random numbers. Call this function before calling any other random module function. Returns None.
|
5
|
shuffle(lst)
Randomizes the items of a list in place. Returns None.
|
6
|
uniform(x, y)
A random float r, such that x is less than or equal to r and r is less than y
| Trigonometric Functions
Python includes following functions that perform trigonometric calculations.
Sr.No.
|
Function & Description
|
1
|
acos(x)
Return the arc cosine of x, in radians.
|
2
|
asin(x)
Return the arc sine of x, in radians.
|
3
|
atan(x)
Return the arc tangent of x, in radians.
|
4
|
atan2(y, x)
Return atan(y / x), in radians.
|
5
|
cos(x)
Return the cosine of x radians.
|
6
|
hypot(x, y)
Return the Euclidean norm, sqrt(x*x + y*y).
|
7
|
sin(x)
Return the sine of x radians.
|
8
|
tan(x)
Return the tangent of x radians.
|
9
|
degrees(x)
Converts angle x from radians to degrees.
|
10
|
radians(x)
Converts angle x from degrees to radians.
| Mathematical Constants
The module also defines two mathematical constants −
Sr.No.
|
Constants & Description
|
1
|
pi
The mathematical constant pi.
|
2
|
e
The mathematical constant e.
|
Do'stlaringiz bilan baham: |