#! /usr/bin/env python # -*- coding: utf-8 -*- """ Code from the chapter 2 on "Python in Easy Steps" book. Can be downloaded from http://ineasysteps.com/resource-centre/downloads/ (http://ineasysteps.com/wp-content/uploads/2013/07/src.zip) Credit: Lilian Besson for http://perso.crans.org/besson/cs101/solutions/easysteps/ License: GPLv3 """ # Page 27 print "# Doing arithmetic" a, b = 8, 2 print "Addition: \t", a, "+", b, "=", a + b print "Substraction: \t", a, "-", b, "=", a - b print "Product: \t", a, "*", b, "=", a * b print "Division: \t", a, "/", b, "=", a / b print "Division: \t", a, "//", b, "=", a // b print " (Forced to be an integer, with the floor function)" print "Modulus: \t", a, "%", b, "=", a % b print "Exponent: \t", a, "**", b, "=", a ** b # Page 29 print "# Assigning values" a, b = 8, 4 print "\n\nAssign values: \t\t a=", a, "\tb=", b a += b print "Add and assign (a += b):\t\t a=", a, "(8 + 4)" a -= b print "Substract and assign (a -= b):\t\t a=", a, "(12 - 4)" a *= b print "Multiply and assign (a *= b):\t\t a=", a, "(8 * 4)" a /= b print "Substract and assign (a /= b):\t\t a=", a, "(32 / 4)" a %= b print "Multiply and assign (a %= b):\t\t a=", a, "(8 % 4)" # Page 31 print "# Comparing values" nil, num, big, cap, low = 0, 0, 1, "A", "a" print "Equality:\t", nil, "==", num, nil == num print "Equality:\t", cap, "==", low, cap == low print "Difference:\t", nil, "!=", big, nil != big print "Greater:\t", nil, "<", big, nil < big print "Smaller:\t", nil, ">", big, nil > big print "More or equal:\t", nil, ">", big, nil < big print "Less or equal:\t", nil, "<", big, nil > big # Page 33 print "# Assessing logic (a = True, b = False)" a, b = True, False print "\nAND logic (operator 'and'):" print "a and a = ", a and a print "a and b = ", a and b print "b and b = ", b and b print "\nOR logic (operator 'or'):" print "a or a = ", a or a print "a or b = ", a or b print "b or b = ", b or b print "\nNOT logic (operator 'not'):" print "not a = ", not a print "not b = ", not b # Page 35 print "# Examining conditions" a, b = 1, 2 print "Variable a is", "one" if a == 1 else "not one" print "Variable a is", "even" if a % 2 == 0 else "odd" print "Variable b is", "one" if b == 1 else "not one" print "Variable b is", "even" if b % 2 == 0 else "odd" # Page 35 print "# Setting precedence" a, b, c = 2, 4, 8 print "\nDefault order:\t", a, "*", c, "+", b, "=", a * c + b print "Forced order:\t", a, "* (", c, "+", b, ") =", a * (c + b) print "\nDefault order:\t", c, "//", b, "-", a, "=", c // b - a print "Forced order:\t", c, "// (", b, "-", a, ") =", c // (b - a) print "\nDefault order:\t", c, "%", a, "+", b, "=", c % a + b print "Forced order:\t", c, "% (", a, "+", b, ") =", c % (a + b) print "\nDefault order:\t", c, "**", a, "+", b, "=", c ** a + b print "Forced order:\t", c, "** (", a, "+", b, ") =", c ** (a + b) # Page 35 print "# Casting data types" a = input("Enter a NUMBER: ") b = input("Enter a second NUMBER: ") sumab = a + b print "Data Type sum : ", sumab, type(sumab) sumab = int(a) + int(b) print "Data Type sum : ", sumab, type(sumab) sumab = float(sumab) print "Data Type sum : ", sumab, type(sumab) sumab = chr(int(sumab)) print "Data Type sum : ", sumab, type(sumab) # Page 41 print "# Manipulating bits" a, b = 10, 5 print "a = ", a, "and b = ", b # 1010_2 ^ 0101_2 = 1111_2 (decimal 15) a = a ^ b # 1111_2 ^ 0101_2 = 1010_2 (decimal 10) b = a ^ b # 1111_2 ^ 1010_2 = 0101_2 (decimal 5) a = a ^ b print "a = ", a, "and b = ", b if __name__ == '__main__': from doctest import testmod testmod(verbose=False)