Things Lab organized a simple training in Python of the Week of Code. We wrote some simple Python code, explaining basics of programming with some simple math examples
version • Backward compatible • A lot of libraries, making it extendable Python v3.x • The present and the future • More updated than v2.x • Standard library improvements • Backwards incompatible
use the input() function # to get string input from the user user = input("What's your name? ") # reading the user's name # By using %s for strings, %d for ints and %f for FP, # we can pre-format strings print("Hello %s, it's so nice to meet you." % user) # We use the int() function to parse an integer out of a # string. In this case, we are parsing the input string age = int(input("How old are you? ")) print("So you were born in %d? That's nice" % (2014 - age)) print("I was born on 1991, so I'm 23 years old.") print("We can learn so much together")
math.pi print("Our circle of radius %d has:" % radius) print(" - Perimeter of %f" % (2*PI*radius)) print(" - Area of %f" % (PI * radius**2)) # # # # # # # # # # # # # # # # # # # # # # # # # a = float(input("Side 1 of triangle: ")) b = float(input("Side 2 of triangle: ")) c = float(input("Side 3 of triangle: ")) p = (a + b + c ) / 2 area = math.sqrt(p * (p-a) * (p-b) * (p-c)) print("The area of the triangle is %f" % area)
2. Feet to meters converter 3. **Sum of digits of a number in [0; 1000] 4. *Compute BMI 5. *Calculate compound interest Don’t forget: GIYF (Google Is Your Friend)
0") a = float(input("a: ")) b = float(input("b: ")) c = float(input("c: ")) D = b**2 - 4*a*c if D > 0: # two real roots x1 = (-b + math.sqrt(D))/(2*a) x2 = (-b - math.sqrt(D))/(2*a) print("Two real roots:") print("x1 = %f" % x1) print("x2 = %f" % x2) elif D == 0: # one real root x = (-b)/(2*a) print("Only one real root:") print("x = %f" % x) else: print("No real roots")
BMI 2. **Use Zeller’s congruence to calculate the day of the week for a given date 3. *Rock, Scissor, Paper game 4. Randomly pick two numbers < 100 and ask the user to add, subtract and multiply them. For every correct result, give him 1 mark. Print the marks in the end.
number = randint(1,100) while True: guess = int(input("Guess the number: ")) if guess > number: print("Wrong! Your guess is too high!") elif guess < number: print("Wrong! Your guess is too low!") else: print("***CORRECT!***") break
ask the user for their level. Based on the level, it should ask arithmetic question regarding addition, subtraction and multiplication (chose by the user). In the end, it should give the result of the test, which is “Well Done” for more than 75%, “Good” for more than 50%, and “Bad” for less than 50%. For the Beginner level, the numbers should be 10, for Medium 50, and for Advanced 100.