Chapter 4: Basic declarations and expressions

Example 4.1, Page number: 71

In [1]:
# Variable declaration
x = (1 + 2) * 4

# Result
print ('1 plus 2 multiplied by 4 gives %d' % x)
1 plus 2 multiplied by 4 gives 12

Example 4.2, Page number: 75

In [2]:
# Variable declaration
term = 3 * 5
term_2 = 2 * term
term_3 = 3 * term

# Result
print ('Twice %d is %d' % (term, term_2))
print ('Three times %d is %d' % (term, term_3))
Twice 15 is 30
Three times 15 is 45

Example 4.3, Page number: 77

In [3]:
# Variable declaration
term = 3 * 5
term_2 = 2 * term
term_3 = 3 * term

# Result
print ('Twice %d is %d' % (term, term_2))
print ('Three times %d is %d' % (term, term_3))
Twice 15 is 30
Three times 15 is 45

Example 4.4, Page number: 79

In [4]:
# Variable declaration
answer = 1/3

# Result
print ('The value of 1/3 is %f' % answer)
The value of 1/3 is 0.000000

Example 4.5, Page number: 80

In [5]:
answer = 2 + 2

# Result
print ('The answer is %d' % answer)
The answer is 4

Example 4.6, Page number: 80

In [6]:
# Variable declaration
result = 7.0/22.0

# Result
print ('The result is %d' % result)
The result is 0

Example 4.7, Page number: 82

In [7]:
# Variable declaration
char1 = 'A'
char2 = 'B'
char3 = 'C'

# Result
print ('%c%c%c reversed is %c%c%c' % (char1, char2, char3, char3, char2, char1))
ABC reversed is CBA