TIME NOW CALENDAR CONVERT LENGTH CONVERT TEMPERATURE DICTIONARIES, LISTS SCIENCE EDUCATION RELIGION WORK CALCULATOR CALCULATE LIFE

# Python 3 programming language, learn python, tutorial

ALL TOPICS

## Python 3. Type conversions. Numbers and methods

Types
• int (signed integers): positive or negative whole numbers.
• long (long integers ): integers of unlimited size, followed by an uppercase or lowercase L.
• float (floating point real values) : with a decimal point dividing the integer and fractional parts.
• complex (complex numbers) :  a + bJ, where a and b are floats and J (or j).
Conversion
int(x [,base]) x to an integer. base specifies the base if x is a string.

long(x [,base] ) x to a long integer. base specifies the base if x is a string.

float(x) x to a floating-point number.

complex(real [,imag]) Creates a complex number.

str(x) object x to a string representation.

repr(x)  object x to an expression string.

eval(str) Evaluates a string and returns an object.

tuple(s) s to a tuple.

list(s) s to a list.

set(s) s to a set.

dict(d) Creates a dictionary. d must be a sequence of (key,value) tuples.

frozenset(s)  s to a frozen set.

chr(x) an integer to a character.

unichr(x) an integer to a Unicode character.

--- ord(x) a single character to its integer value.

hex(x) an integer to a hexadecimal string.

oct(x) an integer to an octal string.

1.:
a="1"
b= float(a) * 1.1
print ("<div>" + str(b) + " EUR</div>")

Result:
<div>1.1 EUR</div>

2.:
a="1"
b= a * 1.1
print ("<div>" + str(b) + " EUR</div>")

Result:
Traceback (most recent call last):
File "new.py", line 2, in <module>
b= a * 1.1
TypeError: can't multiply sequence by non-int of type 'float'

3. Strings to python:
x = 1

# expression
x = eval('x+1')
print (x)

# error
# eval('x = x+1')

# execution
exec('y=x+1')
print (y)

# creating expression row
i=10
print (eval('"x"+str(i)+"="+"123 + 1"'))

# eval and exec
i=10
exec(eval('"x"+str(i)+"="+"123 + 1"'))
print (x10)
Math
abs(x), ceil(x), cmp(x, y), exp(x), fabs(x), floor(x), log(x), log10(x), max(x1, x2,...) , min(x1, x2,...), modf(x), pow(x, y), round(x [,n]), sqrt(x)
Math operations:
• =       py = a + b assigns value of a + b into py
• +=    py += a is equivalent to py = py + a
• -=      py -= a is equivalent to py = py - a
• *=      py *= a is equivalent to py = py * a
• /=      py /= a is equivalent to py = py / a
• %=      py %= a is equivalent to py = py % a
• **=      py **= a is equivalent to py = py ** a
• //=      py //= a is equivalent to py = py // a
3.:
from math import *

print (ceil(10.1))

4. Chained comparison:
X=1
Y=2
Z=3

print (X<Y<Z)
print (X<Y and Y<Z)

4b. Chained comparisons:
X=1
Y=2
Z=3

print (X<Y>Z)
print (X>Y and Y<Z)

4c. Chained comparisons:
print ( 1 < 2 < 3.0 < 4)

5. Integrated functions:
# Hex
print(hex (7))

# No sign
print(abs (-100))

# (a // b, a % b)
print(divmod(37, 27))
print(divmod(5, 6))

# round
print(round(2.6799, 2))

6. Math module:
from math import *

# minus number?
x = -11;
print(isinf(x))

# sin
y = 90;
print(sin(y))

# false if number
c = 2;
print(isnan(c))

---
ALL TOPICS