Def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50))

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Container Types in Python
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Def f(x): counter = 0 y = 0; while counter
Def f(x): if (x == 1): return x else: return(x + f(x-1)) assertEqual(f(4),______) def f2(x): if (x == 1): return str(x) else: return(str(x) + f2(x-1))
Def f(x): counter = 0 y = 0; while counter
Guide to Programming with Python
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Programming for Linguists An Introduction to Python 24/11/2011.
PPT 2. Other things: functions  Can you have a function with no inputs?  Yes: def f( ): return (3 + 4 )  Can you have a function with no outputs? 
Computer Science 111 Fundamentals of Programming I The while Loop and Indefinite Loops.
PPT3. Quick function:  Write a function that checks to see if a number is even or not.  What TYPE does this return?
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
PPT 4. Variables: (assignment)  A variable: holds a value.  A space in RAM we give a name to  A lot like parameters, only we create them within the.
Last Week if statement print statement input builtin function strings and methods for loop.
6.2 For…Next Loops General Form of a For…Next Loop
More about Strings. String Formatting  So far we have used comma separators to print messages  This is fine until our messages become quite complex:
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Strings:  Strings?  concatenate (join) +  make multiple copies using *  compare: > =
Decision Structures, String Comparison, Nested Structures
Largest of x random numbers def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count.
By Austin Laudenslager AN INTRODUCTION TO PYTHON.
Mr. Fowler Computer Science 14 Feb 2011 Strings in Python.
Last Week Modules Save functions to a file, e.g., filename.py The file filename.py is a module We can use the functions in filename.py by importing it.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 11 Using strings and sequences 5/02/09 Python Mini-Course: Day 3 – Lesson.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
COP 2510 Chapter 6 - Functions. Random function (randint) #import the desired function import random #integer random number in the range of 1 through.
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
Computer Programming for Engineers
ITEC 109 Lecture 20 Arrays / Lists. Arrays Review For loops –Rationale –Syntax –Examples.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming 1.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Strings … operators Up to now, strings were limited to input and output and rarely used as a variable. A string is a sequence of characters or a sequence.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Mr. Crone.  Use print() to print to the terminal window  print() is equivalent to println() in Java Example) print(“Hello1”) print(“Hello2”) # Prints:
Computer Science 18 Feb 2011 Strings in Python. Introduction Prior experience  Defining string variables  Getting user input  Printing strings Lesson.
LISTS: A NEW TYPE! We've got: ints floats booleans strings and now… (drum roll) LISTS! sets of a thing (e.g., sets of ints)
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Guide to Programming with Python Chapter Four Strings, and Tuples; for Loops: The Word Jumble Game.
Unit 4 – Chapter 4 Strings and Tuples. len() Function You can pass any sequence you want to the len() function and it will return the length of the sequence.
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
For loop: our last loop type
String and Lists Dr. José M. Reyes Álamo.
Scientific Programming in Python -- Cheat Sheet
Python unit_4 review Tue/Wed, Dec 1-2
Other things: functions
Introduction to Strings
Introduction to Strings
Chapter 10 Lists.
String and Lists Dr. José M. Reyes Álamo.
Remembering lists of values lists
Introduction to Strings
(more) Python.
For loops Taken from notes by Dr. Neil Moore
What is printed out? def f(x): print(x+3) return(x + 7) def g(k): return(f(k) + 2) print(g(1)) 4 10 def f2(x): return(x + 7) print(x+3) def g2(k): return(f2(k)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Strings: What can we do with Strings?
More While Loops.
Introduction to Strings
Lists: a new type! LISTS! sets of a thing (e.g., sets of ints)
Strings: Strings? concatenate (join) + make multiple copies using *
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Basics of Python Common types of data we will work with
Data Types String holds alphanumeric data as text.
Presentation transcript:

def find_largest(x,y): largest = 0; count = 0; while count < x: rnum = randrange(0,y) if rnum > largest: largest = rnum count +=1 return(largest); print(find_largest(7,50)) Largest of x random numbers Write a function that takes 2 integers (x and y) as input parameters, and generates x random numbers between 0 and y. It finds the largest of those random numbers.

Try: Multiplication tables Write a function that takes as an input parameter a number, and then prints out that number's multiplication table up through 10, e.g., if the number was 4, you'd get … 40 (Or Fancier version): 0x4=0 1x4=4 2x4=8 3x4=12 … 10x4=40 def func(x): count = 0 while (count <= 10): print(count*x) count += 1 return (total) print(func(5)) def fancyfunc(x): count = 0 while (count <= 10): print(str(count)+"x"+str(x)+"="+str(count*x)) count += 1 return (total) print(fancyfunc(7))

from random import * def beloworabove(): count = 0 below = 0 above = 0 while (count < 1000): rand = randrange(-100,100) if (rand < 0): below += 1 else: above += 1 count += 1 if below > above: return("generated " + str(below) + " numbers below 0") else: return ("generated " + str(below) + " numbers above 0") print(beloworabove()) Average: count Write a function that takes no inputs. It generates 1000 random numbers between -100 and 100. It counts how many of the random numbers are below 0 and how many are at or above 0. It then tells you which is more likely, below 0 or at or above 0. Starting condition? What makes the loop stop? What inside the loop changes so that the loop will stop?

What do we get? from math import * def f(a,b): n = min(a,b) i = 1 g = 1 while (i <= n): if (a%i == 0) and (b%i == 0): g = i i = i+1 return(g) print(f(32,24)) print(f(13,8)) print(f(24,16))

For loop: another type of loop We use For loops when we know exactly how many times the loop will occur Form: for variable in [value1, value2,value3…lastvalue]: calculations Example: def f(): for x in [1,2,3,4,5]: print(x) return(x) print(f())

More for loops: def f(): for x in [1,3,5,7,9]: print(x) return(x) print(f()) def f(): for x in [2,7,1,9,4]: print(x) return(x) print(f())

More for loops: def f(): y = 0 ct = 0 for x in [3.2, 7.1, 8.0, 3.4, 5.1]: print("including " + str(x)) ct +=1 y = y + x return(y/ct) print(f())

Loops with strings: def f(y): ct = 0 for x in ["puppy","bunny","puppy","bird","echidna","puppy"]: if x == y: ct += 1 return(ct) print(f("puppy"))

More for loops: def f(): for x in [0,1,2,3,4]: print(x) return(x) print(f()) Shortcut: using range def f(): for x in range(5): # range(5) = [0,1,2,3,4] print(x) return(x) print(f())

Same? def forfunc(y): total = 0 for x in range(y): total += x return(total) print(forfunc(5)) def whilefunc(y): count = 0 total = 0 while (count < y): total += count count += 1 return (total) print(whilefunc(5))

More on Range: def f(): for x in range(-3,3): # from -3 up to but not including 3, e.g., [-3,-2,-1,0,1,2] print(x) return(x) print(f()) def f(): for x in range(-3,3,2): # from -3 up to but not including 3, by increments of 2, e.g., [-3,-1,1] print(x) return(x) print(f()) (Can we make a loop go backwards?)

What does this do? def f(): y = 1000 total = 0 for x in ["2","7","1","9"]: total = total + int(x) * int(y) y /=10 return(total) print(f())

What does this do? def f(z): y = int(input("enter a number")) for x in range(y): print (str(z)+ "*"+str(x)+"=\t"+str(x*z)) print ("\n") return f(4)

How about this? def f(z): y = int(input("enter a number: ")) for x in range(1,y): for q in range(1,z): print (str(q)+ "*"+str(x)+"=\t"+str(x*q)) print ("\n") return f(4)

Just about anything you can do with lists: Len, in def f(x): if "e" in x: return("e is in your message.") else: return("e is not in your message.") strvar = “puppies are cute” print(f(strvar)) z = len("cat")

Positions def f(wv): return(wv[3]) wordvar = “cats” print(f(wordvar))

Slicing (Different from Indexing) Copying parts of strings: | p | i | z | z | a | def f(): word = “pizza” return(word[0:5]) def g(): word = “pizza” return(word[1:3]) def h(): word = “pizza” return(word[-4:-2]) def i(): word = “pizza” return(word[-4:3])

Shortcuts | p | i | z | z | a | word=“pizza” word[0:4] pizz word[:4] pizz word[2:5] zza word[2:] zza

# display a slice def g(s,f,wd): return("wd["+str(s)+":"+str(f)+"] is "+str(wd[s:f])) print(g(3,7,"sesquipedalian"))

Strings are Immutable word of the day Can: x = "catamaran" print(x.count("a")) print(x.index('a')) Can’t (if it changes the string, we can’t do it) Append (use + instead) Reverse() Pop() Insert() Sort()

What does this do? def f(str1): str2 = “WuGmUmP” if str1.upper() == str2.upper(): return(“You’re one of us!”) else: return(“You’re not one of us!”) newstr = “wuGMuMp” print(f(newstr))

What does this do? def f(): strvar = input("enter a string: ") var1 = "" for x in range(len(strvar) - 1,-1,-1): var1 += strvar[x]; return(var1) print(f())

This one? def f(): strvar = input("enter a number: ") y = 1 z = 0 var1 = "" for x in range(len(strvar) - 1,- 1,-1): z += int(strvar[x]) * y; y*=10 return(z) print(f())

What does this give you? def f(lv): x = len(lv) print(x) for y in range(0,x): if "t" in lv[y]: print(lv[y]) return listvar = ["ham","am","boat","goat","there","anywhere"] f(listvar)

How about this one? def f(word): newstr = "" for i in range(1,len(word)+1): newstr += word[-i] return(newstr) wvar = "sesquipedalian" print(f(wvar))

What does this do? def f(word): high = len(word) low = 0 newstr = "" for i in range(10): position = randrange(low, high) newstr += word[position] return(newstr) wvar = "sesquipedalian" print(f(wvar))

How about this one? def f(m): new_m = "" SPECIAL = "dlmstp" for k in m: if k.lower() not in SPECIAL: new_m += k return(new_m) mvar = "Hi, my name is Lassie" print("Your message is: now" + f(mvar))

Something you can’t do word = “ night”; word[0] = “s”; Instead: newword = “” for x in “night”: if x == “n": newword += “s" else: newword += x

What do you get? def f(message): newmessage = "" for x in message: if x == "g": newmessage += "l" else: newmessage += x return(newmessage) mvar = "pogysyggabicaggy" print("new string: " + f(mvar))