


















Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!



















A detailed, step-by-step set of instructions telling a computer what to do. If we change the program, the computer performs a different set of actions or a different task. The machine stays the same, but the program changes! SOFTWARE DEVELOPMENT PROCESS
def main(): n = eval(input("Please enter a whole number: ")) fact = 1 for factor in range(n,1,-1): fact = fact * factor print("The factorial of", n, "is", fact) main()
def main(): print("This program allows you to total up some numbers") print() n = int(input("How many numbers do you have? ")) total = 0 for i in range(n): num = float(input("Enter a number: ")) total = total + num print() print("The sum of the numbers is:", total) main()
import math def main(): print("This program calculates square root using Newton's method.") print() x = float(input("Enter number to find the root of: ")) n = int(input("How many iterations should I use? "))
guess = x / 2. for i in range(n): guess = (guess + x/guess)/2. print() print("Approximate square root:", guess) print("Difference from math.sqrt:", math.sqrt(x) - guess) main() GRAPHICS >>> ### Open a graphics window >>> win = GraphWin('Shapes') >>> ### Draw a red circle centered at point >>> ### (100, 100) with radius 30 >>> center = Point(100, 100) >>> circ = Circle(center, 30) >>> circ.setFill('red') >>> circ.draw(win) >>> ### Put a textual label in the center of the circle >>> label = Text(center, "Red Circle") >>> label.draw(win) >>> ### Draw a square using a Rectangle object >>> rect = Rectangle(Point(30, 30), Point(70, 70)) >>> rect.draw(win) >>> ### Draw a line segment using a Line object >>> line = Line(Point(20, 30), Point(180, 165)) >>> line.draw(win) >>> ### Draw an oval using the Oval object >>> oval = Oval(Point(20, 150), Point(180, 199)) >>> oval.draw(win) GUI Most applications you’re familiar with have Graphical User Interfaces (GUI) that provide windows, icons, buttons and menus.
main()
def main():
seconds = int(input("Enter the number of seconds: "))
hrs = seconds // (60*60)
rem_sec = seconds % (60*60)
min = rem_sec // 60
sec = rem_sec % 60
print(seconds, " seconds correspond to ", hrs, ":", min, ":", sec, sep='') main()
def main(): inputNumber = float(input("Enter the course grade: ")) grade = int(inputNumber) if grade > 30 or grade < 18 or grade != inputNumber: print("Error: grade must be an integer") print(" between 18 and 30") else: print("Grade", grade, "accepted.")
main()
prime, otherwise it returns False
values def primality_test(n): prime_flag = True # this "flag" marks whether the number n is prime or not
for d in range(2,n):
Int() = convert expr to a integer value; “” Se inserisco valori misti, trasforma da int a float Str() = return a string representation of expr Eval() = evaluate string as a expression PARAMETERS – FORMAL AND ACTUAL VS VARIABLED The main purpose is to provide information from the caller to the function. Parameters provide the changeable context of a function call. The scope of a variable refers to the places in a program where it can be accessed Four step process The calling program (the caller) suspends its execution at the point of the call. The formal parameters of the called function get assigned the values supplied by the actual parameters in the call. The body of the function is executed. Control moves back to the point of the caller just after where the function was called. Formal parameters are variables in the function definition. Actual parameters are the values given to those variables when control transfers to the function. Difference between ordinary variables and parameters Parameters are like ordinary function variables in that they are local to the function and serve as "named values." They are different because they are automatically
assigned an initial value when the function is called. Normal local variables have to be assigned a value within the function definition. Unicode (ord, chr) standard encoding/decoding: The ord (for ordinal) function returns the numeric code of a single character The chr function converts a numeric code to the corresponding character. STRING METHOD s.capitalize() – Copy of s with only the first character capitalized s.title() – Copy of s; first character of each word capitalized s.center(width) – Center s in a text field of given width: s.count(sub) – Count the number of occurrences of substring sub in s s.find(sub) – Find the first position where sub occurs (starts) in s returns - 1 if sub does not occur in s s.join(list) – Concatenate list of strings into one large string using s as separator s.ljust(width) – Like center, but s is left-justified s.lower() – Copy of s in all lowercase letters s.lstrip() – Copy of s with leading whitespace(s) removed s.replace(oldsub, newsub) – Replace occurrences of oldsub in s with newsub s.rfind(sub) – Like find, but returns the right-most position
rev_word = reverse_word(word) return rev_word == word
unimodal or not def is_nondec(seq):
l = len(seq) if l <= 1: return True else: nondec_flag = True
for i in range(0,l-1): if seq[i] > seq[i+1]: nondec_flag = False return nondec_flag def is_noninc(seq):
l = len(seq) if l <= 1: return True
else: noninc_flag = True
for i in range(0,l-1): if seq[i] < seq[i+1]: noninc_flag = False return noninc_flag def unimodal(seq): found_unimodal = False
for pos in range(0,len(seq)):
decreasing seq1 = seq[:pos] seq1_nondec = is_nondec(seq1)
seq2 = seq[pos:] seq2_noninc = is_noninc(seq2) if seq1_nondec and seq2_noninc: found_unimodal = True return found_unimodal # Notice: thanks to for-loop above, this returns True iff the two checks of at least one iteration are successful