Agenda ints, floats, strings, booleans Conditionals Loops Functions Testing The concept of scope
Python types 4 - int 4.5 - float ‘abcdefgh’ - string True – boolean False - boolean When in doubt use the type function type(45)
Variables in python Variables are used to keep track of information within a program An assignment statement is of the form Variable name = value Proper naming of variables is important. Variables that store the first, second and third highest scores in a test a, b, c - bad names first, second, third - better Randomly named variables will result in loss of points in assignments You cannot use space, but can choose between second_best or secondBest(camel casing) “ Programs must be written for people to read, and only incidentally for machines to execute. ” - Abelson
Python variable zen Python allows for multiple variables to be assigned to the same value a = b = c = 42 Python allows you to change the type of a variable on the fly A = 42 A = ‘forty two’ A = True Swapping variable values in Python is cool!!! x,y = y, x
Conditionals If else elif See guessingGame_v1.py in the repository
Inbuilt functions in python Similar to the math concept of functions Functions on strings Functions that convert from one data type to another The fun ‘eval’ function
One big giant python file …. Python is full of modules import math from math import * the same logic can be used for your own function Example areaFuncs.py and usageOfFuncs.py
Abstraction A good program will have layers of abstraction More often that not you only care about the input and output and do not really want or even need to know the internal workings A well written function is like a blackbox converting input into meaningful output math.sqrt() - how is implemented Who cares!
Modular programming Software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules each module contains everything necessary to execute only one aspect of the desired functionality. You’ll also hear this referred to as top-down design and stepwise refinement
functions In accordance with the concept of abstraction Divide a program up into its ‘functional’ portions Testing becomes much easier Test each component really well – more on this later … Think about a few integration tests vowelCounter.py
Information hiding David Parnas first introduced the concept of information hiding around 1972. hiding "difficult design decisions or design decisions which are likely to change." Hiding information isolates clients from requiring intimate knowledge of the design to use a module, and from the effects of changing those decisions. Modular programming goes hand in hand with information hiding Whoever used your function is just relying on getting the right output for their input. So we can change the internal implementation safely
Software reuse Do not reinvent the wheel The reuse of programming code is a common technique which attempts to save time and energy by reducing redundant work. Software libaries/modules - good example of code reuse In general if you are writing a program and you find yourself copy pasting code it is probably time to write a function.
Good program design Separate your program into the building blocks Write a function for each of the building blocks Simple methods of separation Separate functions for the calculations Separate function for the input Separate function for the output
The coke machine example Cokemachine1 and cokemachine2.py Modular code Easier to read Easier to test Easier to communicate across ‘departments’ of your company
More modular programming Using romanNumeral.py as an example Is there a pattern that repeats itself when we are working with roman numerals If you see a common pattern/repeated usage of the same code – convert that into a function!
Scoping local variables versus global variables changing the value of a global variable within a function using the global keyword Once you use the global keyword, you are changing the global variable as opposed to the variable that is just scoped to the function
Your consent to our cookies if you continue to use this website.