Chapter 2: Variables and Expressions

Example integer.c, page no. 35

In [1]:
val = 10
print "%d" %val
10

Example float.c, page no. 36

In [3]:
i_var = 10
f_var = 100.3125
print "i_var is %d and f_var is %f" %(i_var, f_var)
i_var is 10 and f_var is 100.312500

Example decimal.c, page no. 37

In [1]:
inp_int = int(raw_input())
print "The input integer (in decimal) is %u" %inp_int
print "The input integer (in hexadecimal) is %x" %inp_int
45
The input integer (in decimal) is 45
The input integer (in hexadecimal) is 2d

Example scanf.c, page no. 37

In [10]:
years = float(raw_input("Input your age in years: "))
secs = years*365*24*60*60
print "You have lived for %0.1f seconds" %secs
Input your age in years: 21
You have lived for 662256000.0 seconds

Example char.c, page no. 38

In [12]:
c = '#'
print "This is the hash symbol: %s" %c
This is the hash symbol: #

Example ascii.c, page no. 39

In [14]:
code = int(raw_input("Input an ASCII code (0 to 127):" ))
symbol = chr(code)
print "The symbol corresponding to %d is %s" %(code,symbol)
Input an ASCII code (0 to 127):75
The symbol corresponding to 75 is K

Example string.c, page no. 40

In [1]:
my_string = "13 is unlucky."
print "my_string: %s" %my_string
my_string: 13 is unlucky.

Example size.c ,page no. 41

In [27]:
import sys
print "Size of a int is %d" %sys.getsizeof(int)
print "Size of a long int is %d" %sys.getsizeof(long)

#Note: there is no short interger data type in Python
#Answers will differ
Size of a int is 872
Size of a long int is 872

Example inout.c, page no. 42

In [3]:
shorti = int(raw_input("Input short int: "))
longi = int(raw_input("Input long int: "))
print "shorti = %d and longi = %d" %(shorti, longi)
Input short int: 12
Input long int: 200000000
shorti = 12 and longi = 200000000

Example beep.c, page no. 47

In [4]:
print "%c" %('\x07')

#note: there won't be any beep in Python


Example tax.c, page no. 49

In [7]:
sal = float(raw_input("Salary: "))
tax = sal * 0.3
print "tax = %.1f" %tax
Salary: 200000
tax = 60000.0

Example tax1.c, page no. 49

In [8]:
tax_rate = 0.3
sal = float(raw_input("Salary: "))
tax = sal * tax_rate
print "tax = %.1f" %tax
Salary: 200000
tax = 60000.0

Example const.c, page no. 50

In [9]:
tax_rate = 0.3
sal = float(raw_input("Salary: "))
tax = sal * tax_rate
print "tax = %.1f" %tax

#Note: there is no constant data type in Python so the program will remain same as above
Salary: 200000
tax = 60000.0

Example const.c, page no. 50

In [10]:
tax_rate = 0.3
sal = float(raw_input("Salary: "))
tax = sal * tax_rate
print "tax = %.1f" %tax

#Note: there is no #define preprocessor in Python so the program will remain same as above
Salary: 200000
tax = 60000.0

Example relation.c, page no. 53

In [11]:
print "Input two numbers"
i = int(raw_input("num1: "))
j = int(raw_input("num2: "))
if (i == j):
    print "They are equal."
print "Got the message?"
Input two numbers
num1: 34
num2: 34
They are equal.
Got the message?

Example char.c, page no. 54

In [22]:
c = 255
d = -1
if c < 0:
    print "c is less than 0"
else:
    print "c is not less than 0"
if d < 0:
    print "d is less than 0"
else:
    print "d is not less than 0"
    
#the program will not have output as that given in the textbook
#because, there is no concept of char, signed or unsigned values in Python
c is not less than 0
d is less than 0

Example char8.c, page no. 55

In [23]:
c = '255'
d = '-1'
if c < 0:
    print "c is less than 0"
else:
    print "c is not less than 0"
if d < 0:
    print "d is less than 0"
else:
    print "d is not less than 0"

#the program will not have output as that given in the textbook
#because, there is no concept of char, signed or unsigned values in Python
c is not less than 0
d is not less than 0

Example extract.c, page no. 59

In [29]:
i = int(raw_input("Input an integer: "))
n = int(raw_input("Bit position to extract: "))
bit = (i >> n)&1
print "The bit is: %d" %bit
Input an integer: 10
Bit position to extract: 1
The bit is: 1

Example max.c, page no. 62

In [33]:
print "Input two numbers"
i = int(raw_input("num1: "))
j = int(raw_input("num2: "))
larger = i if i > j else j
print "The larger of the two is %d" %larger
Input two numbers
num1: 34
num2: 45
The larger of the two is 45

Example interest.c, page no. 65

In [35]:
p = float(raw_input("Principal: "))
r = float(raw_input("Rate: "))
t = float(raw_input("Time: "))
ncmp_year = float(raw_input("Compoundings per year: "))
simple = p * r * t / 100
cmp_amount = p * pow(1 + r / (ncmp_year * 100), ncmp_year * t)
compound = cmp_amount - p
print "The simple interest is : %f" %simple
print "The compound interest is : %f" %compound
Principal: 1000
Rate: 10
Time: 2
Compoundings per year: 4
The simple interest is : 200.000000
The compound interest is : 218.402898

Example 2.10, page no. 66

In [36]:
c = float(raw_input("Enter temperature in celsius: "))
f = 1.8 * c + 32
print "Equivalent fahrenheit = %f" %f
f = float(raw_input("Enter temperature in fahrenheit: "))
c = (f - 32) / 1.8
print "Equivalent celsius = %f" %c
Enter temperature in celsius: 10
Equivalent fahrenheit = 50.000000
Enter temperature in fahrenheit: 50
Equivalent celsius = 10.000000

Example 2.11, page no. 66

In [39]:
import math
print "Enter the 3 sides: "
a = float(raw_input("side1:"))
b = float(raw_input("side2:"))
c = float(raw_input("side3:"))
peri = a + b + c
s = peri / 2
area = math.sqrt(s * (s - a) * (s - b) * (s - c))
print "Area of the triangle is %f" %area
Enter the 3 sides: 
side1:4
side2:5
side3:6
Area of the triangle is 9.921567

Example 2.12, page no. 67

In [40]:
b = float(raw_input("Enter the base: "))
h = float(raw_input("Enter the height: "))
area = b * h / 2
print "Area of the triangle is %f" %area
Enter the base: 3
Enter the height: 2
Area of the triangle is 3.000000