#Recitation 2 Code #What is the decrementing funtion? #What does this loop do? #Prints even numbers from 2 to 10 #a =2 #while (a<= 10): # a += 2 # print a #For loop. #for i in (2,4,6, 8, 10): # print i #for loops require something that is ennumerable # that is, to take one element after the other and # assign it to (in this case i) ### for loops iterat over enumerable items #inconvenient to enter all the numbers over large amounts #Range function #print range(100) #print range(1, 100) #print range (1, 100, 2) #print range (100, 1, -1) #for i in range (2, 1001, 2): # print i #Tuple #tuple_of_numbers = (3.1459, 2, 1, -100, 100, 240) #tuple_of_strings = ('What', 'is', 'my', 'name?') ##Tules start at index 0 ##print tuple_of_numbers [0] ##print tuple_of_strings [0] ##print tuple_of_strings [-1] ##print tuple_of_strings [-4] #how many elements in the tuple ##print len(tuple_of_numbers) #can hold different types #tuple_of_numbers_and_strings = (3.14159, 'is', 'an imperfect', 'representation') #including other tuples #tuple_of_tuples = (('stuff', 'just', 'got', 'real') #print tuple_of_tuples (0) #print tuple_of_tuples ##Can't do this! Immutable! (i.e. can't change it) ##following example attempts to change an element of the tuple ##tuple_of_numbers (0) = 3 ## tuples can be sliced #print tuple_of_numbers #print tuple_of_numbers [1:3] ##implies to start at index 0 #print tuple_of_numbers [:2] ##implies to begin at 1 and go to end of tuple #print tuple_of_numbers [1:] ##go from 0 to -1 from the end #print tuple_of_numbers [:-1] ##iterate over ##for number in tuple_of_numbers: ## print number #Oddity #print 'Before:', tuple_of_numbers #tuple_of_numbers = tuple_of_numbers + (100, 20) #print 'After:', tuple_of_numbers #it creates a new tuple with 100, 20 added ##Wart. ##Possible to create a tuple with a single element #oopsie = (50) ###Note parentheses is the grouping operator in python ##but also used to make tuples ##this makes integer and assigns to oopsie #print 'oopsie: ', oopsie ##this is what we want #onsie = (50,) #print 'onsie: ', onsie ##Strings ##also immutable; nonscalar #name = 'Mitch' #immutable ##name[0] = 'P' - Werong!! ##you can get to individual characters #print name #print name[0] #print name[1] ##iterate over a string ##for letter in name: ##print letter ##print name [1:3] ##they also have functions #Make upper, lower case ##print name.upper() ##print nam.lower() ##find characters or entire strings ##print name.find('i') ##can replace ##print name.replace('M', 'P') #name = name.replace ('M', 'P') #print name #name=name.lower().replace('t', 'r') #print name ##dir (str) ##returns all symbols that exist within the str object ##help str ##ex #help (str.replace) ##example of using break ##break kicks you out of the innermost loop #x = int(raw_input('Enter an integer: ')) #ans = 0 #While ans**3 < abs(x): # ans = ans +1 # print 'current guess =', ans #if ans*3 |= abs(x) # print x, 'is not a perfect cube' #else: # if x<0 # ans = -ans # print 'Cube root of ' + str (x) + ' is ' + str(ans) ##x= int(raw_input('Enter an integer: ')) #for ans in range (0, abs(x)+1): # if ans**3 == abs(x): # break #if ans**3 |= abs(x): # print x, 'is not a erfect cube' #else # if x<0: # ans = -ans # print 'Cube root of ' + str(x) + ' is ' + str(ans) ## Functions #def cube(number): ## Takes a number and returns the cube of that number. ## Input: number (float or int)int ## Output: number (float) # return number**3 #print cube(3) #print cube(5) #def times2(number): ## Takes a number and doubles it ## Input: number (float or int) ## Output: number (float)*** # answer = number * 2 # return answer #print times2(10) #variable scope all_hope = 'Here be dragons' def all_your_vars_are_belong_to_us(variables): ##Steals all your variables. #Input: variables (stuff) #Output: None (it's stolen) my_variable = 'Make your time' print 'parameter passed into the function: ', variables print 'Global variable: ', all_hope print 'Local variable:', my_variable old_meme_is_old = 'Somebody set up us the bomb' all_your_vars_are_belong_to_us(old_meme_is_old) #print my_variable ##This will give an error - my_variable has local scope
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question