Chapter 1 Numbers & Expressions, 'Beginning Python From Novice to Professional' / Цифры и выражения
Addition, subtraction, сложение, вычитание
>>> 2+2
4
>>>3-2
1
(!) Division, деление
>>>1/2
0
>>>1.0/2.0
0,5
>>>1/2.
0.5
_______________________________________________________________________________
From _future_import division
>>>1/2
0.5
>>>1//2
0
_______________________________________________________________________________
Reminder operator / оператор остатка
>>>1%2
1
>>> 10%3
1
>>> 10%4
2
>>> 10%5
0
_______________________________________________________________________________
Multiplication / умножение
>>> 3*333
999
_______________________________________________________________________________
Exponentiation / Возведение в степень
>>> 2**3
8
>>> -3**2
-9
>>> (-3)**2
9
_______________________________________
Variables
Assignment
x = 3
Expression
x*2
_______________________________________
Statements—the instructions
>>>print 2*2
4
______________________________________________________
Getting input from user ( привет, "По галактике автостопом")
>>> input ('The meaning of life: ')
The meaning of life: 42
42
>>> x = input ('x: ')
x: 30
>>> y = input ('y: ')
y: 40
>>> x*y
1200
____________________________________________________
Functions
>>> 2**3
8
>>> x=pow(2,3)
>>> x
8
>>> 10+pow(2,3*5)/3
10932
____________________________________________________
Absolute number
>>> abs(-10)
10
Rounding
>>> 1/2
0
>>> round (1.0/2.0)
1.0
____________________________________________________________
Modules
>>> import math
>>> math.floor (32.9)
32.0
>>> int(math.floor(32.9))
32
Import only one function
>>> from math import sqrt
>>> sqrt(49)
7.0
SQRT from negative number - imaginary number
>>> import cmath
>>>cmath.sqrt(-49)
7j
_______________________________
Input vs raw_input
>>> name = input ('What is your name?')
What is your name?"Dron"
>>> print "Hello, "+name+"!"
Hello, Dron!
>>> name = raw_input("What isyour name?")
What isyour name?Dron
>>> print "Hello, "+name+"!"
Hello, Dron!
_________________________________________
>>> path = 'C:\nowhere'
>>> path
'C:\nowhere'
>>> print path
C:
owhere
>>> path = 'C:\\nowhere'
>>> print path
C:\nowhere
____________________________________________
>>> print r'C:\nowhere'
C:\nowhere
>>> 2+2
4
>>>3-2
1
(!) Division, деление
>>>1/2
0
>>>1.0/2.0
0,5
>>>1/2.
0.5
_______________________________________________________________________________
From _future_import division
>>>1/2
0.5
>>>1//2
0
_______________________________________________________________________________
Reminder operator / оператор остатка
>>>1%2
1
>>> 10%3
1
>>> 10%4
2
>>> 10%5
0
_______________________________________________________________________________
Multiplication / умножение
>>> 3*333
999
_______________________________________________________________________________
Exponentiation / Возведение в степень
>>> 2**3
8
>>> -3**2
-9
>>> (-3)**2
9
_______________________________________
Variables
Assignment
x = 3
Expression
x*2
_______________________________________
Statements—the instructions
>>>print 2*2
4
______________________________________________________
Getting input from user ( привет, "По галактике автостопом")
>>> input ('The meaning of life: ')
The meaning of life: 42
42
>>> x = input ('x: ')
x: 30
>>> y = input ('y: ')
y: 40
>>> x*y
1200
____________________________________________________
Functions
>>> 2**3
8
>>> x=pow(2,3)
>>> x
8
>>> 10+pow(2,3*5)/3
10932
____________________________________________________
Absolute number
>>> abs(-10)
10
Rounding
>>> 1/2
0
>>> round (1.0/2.0)
1.0
____________________________________________________________
Modules
>>> import math
>>> math.floor (32.9)
32.0
>>> int(math.floor(32.9))
32
Import only one function
>>> from math import sqrt
>>> sqrt(49)
7.0
SQRT from negative number - imaginary number
>>> import cmath
>>>cmath.sqrt(-49)
7j
_______________________________
Input vs raw_input
>>> name = input ('What is your name?')
What is your name?"Dron"
>>> print "Hello, "+name+"!"
Hello, Dron!
>>> name = raw_input("What isyour name?")
What isyour name?Dron
>>> print "Hello, "+name+"!"
Hello, Dron!
_________________________________________
>>> path = 'C:\nowhere'
>>> path
'C:\nowhere'
>>> print path
C:
owhere
>>> path = 'C:\\nowhere'
>>> print path
C:\nowhere
____________________________________________
>>> print r'C:\nowhere'
C:\nowhere