Chapter 2: C Fundamentals

Example pun.c on Page 10

In [1]:
def main():
    print "To C, or not to C: that is the question." #print statement demo
if  __name__=='__main__':
    main()
To C, or not to C: that is the question.

Example on Page 14

In [6]:
#ways for printing on a single line
print "To C, or not to C: that is the question." 
print "To C, or not to C:",
print "that is the question."
To C, or not to C: that is the question.
To C, or not to C: that is the question.

Example dweight.c on Page 20

In [15]:
def main():
    #variable declaration
    height = 8 
    length = 12
    width =10
    volume = height * length * width #volume calculation
    weight = (volume + 165)/166 #weight calculation
    
    #print statements
    print "Dimensions: %dx%dx%d" % (length,width,height) 
    print "Volume (cubic inches): %d" % volume
    print "Dimensional weight (pounds): %d" % weight
    
if  __name__=='__main__':
    main()
Dimensions: 12x10x8
Volume (cubic inches): 960
Dimensional weight (pounds): 6

Example dweight2.c on Page 23

In [17]:
def main():
    #input from user
    height = int(raw_input("Enter height of box: "))
    length = int(raw_input("Enter length of box: "))
    width = int(raw_input("Enter width of box: "))
    volume = height * length * width #volume calculation
    weight = (volume + 165)/166 #weight calculation
    
    #print statements
    print "Volume (cubic inches): %d" % volume
    print "Dimensional weight (pounds): %d" % weight
    
if  __name__=='__main__':
    main()
Enter height of box: 8
Enter length of box: 12
Enter width of box: 10
Volume (cubic inches): 960
Dimensional weight (pounds): 6

Example celsius.c on Page 24

In [4]:
def main():
    #variable declaration
    FREEZING_PT=32.0
    SCALE_FACTOR= 5.02/9.0
    
    #input from user
    farenheit=float(raw_input("Enter Farenheit temperature: "))
    celsius=(farenheit-FREEZING_PT) * SCALE_FACTOR #convert farenheit to celcius
    print "Celsius equivalent: %.1f" % celsius
if  __name__=='__main__':
    main()
Enter Farenheit temperature: 100
Celsius equivalent: 37.9