Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Some hours of python

Some hours of python

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

Aldo Ziflaj

December 13, 2014
Tweet

More Decks by Aldo Ziflaj

Other Decks in Programming

Transcript

  1. What is Python • A high-level programming language • Supports

    multiple paradigms: – Imperative (how-to) programming – Functional programming – Object-Oriented programming (OOP) – Procedural programming • CPython implementation is open source https://github.com/python/cpython
  2. The Zen of Python (5/19) • Beautiful is better than

    ugly • Sparse is better than dense • Simple is better than complex • Complex is better than complicated • Errors should never pass silently
  3. 2or3? Python v2.x • Legacy of the language • Older

    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
  4. What comes next • Installing the Python (3.x) interpreter •

    Greet the console • Crunch some numbers • What if…? • Repeat, repeat, repeat!
  5. Greet the console print("Hello") # This is a comment. We

    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")
  6. Crunch some numbers import math radius = 5 PI =

    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)
  7. Crunch some numbers: self test 1. Fahrenheit to Celsius converter

    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)
  8. What if…? import math print("ax^2 + bx + c =

    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")
  9. What if…? 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")
  10. What if you get yourself tested? 1. *Compute and interpret

    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.
  11. Repeat print("All multiples of 2 AND 3 between 0 and

    99(100-1):") for i in range(100): if i%2 == 0 and i%3 == 0: # even print("%d " % i)
  12. Repeat! from random import randint # pick a random integer

    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
  13. Repeat more! 1. Modify the above program to count how

    many steps did it require to guess the correct number. 2. *Find the sum of digits of any number 3. *Display all prime numbers between 2 and 1000
  14. Final project • Write a mathematical test. The test should

    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.