Python Methods/Functions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

input( )

Function that allows you to prompt the user for input with the argument passed in. Result is always a string.

round( )

Function that allows you to round a number. Takes 2 arguments: the thing being rounded and the number of decimal places.

range( )

Function that gives you a range of numbers, starting at 0 by default. Range is exclusive, not inclusive. You can pass in a single number, resulting in 0 up to but not including that number, two numbers, resulting in the first number up to but not including the end point, or 3 numbers, where the third number is a step size (1 being standard.)

len( )

Function that returns the length of an object. The object must be a sequence or collection.

list( )

Function that turns a range into a list

int( ), float( ), str( )

Functions that allow you to explicitly change the data type of a value.

.pop( ) (dict)

Method that removes the specified value from the dict and returns it. Requires an argument

.get( )

a dict function that will retrieve the specified key's value

.fromkeys( )

a dict method that allows you to create default values for the keys or values. You call this method on an empty list { }, and can put in, for example, the values of [1,2,3] for each key [a,b,c,d,e,f,g]. You can pass in a single value for many keys, multiple values for many keys, or multiple values for 1 key.

.items( )

a dictionary method that allows you to work with both the keys and values of a dictionary, in pairs

.keys( )

a dictionary method that allows you to work with only the keys of a dictionary

.values( )

a dictionary method that allows you to work with only the values of a dictionary

.count( )

a list method that accepts 1 argument, which is the value you want a count for in the list.

.extend( )

a list method that adds all the elements of an iterable (list, tuple, string, etc.) to the end of the list.

.append( )

a list method that adds an item to the end of a list. Takes a single argument.

.index( )

a list method that allows you to get the index position of a specific item (x). You can also pass (x, y), where y is the starting point of search, and (x,y,z), where y and z are the slice of the list you're searching through.

.insert( )

a list method that inserts an item into the specified index. Takes 2 arguments: the index, and the item

.clear( )

a list method that removes all the items from a list. Also works with other collections, I believe

.pop( )

a list method that removes an item at a specified index, and returns it to the user. So you could set a variable equal to list_1([i]), etc. if you pass nothing in, .pop( ) will remove the last item.

.reverse( )

a list method that reverses the items of a list in place. Takes no arguments.

.sort( )

a list method that sorts the items in descending order

.remove( )

a list method that takes a value as an argument, and removes the first item in the collection whose value it is, and only that value. If you want to remove the number 4 from a list, and there are 2 of them, it will remove the first only.

.copy( )

a method that copies a referenced dictionary

.join( )

a string method that also works on lists. takes any iterable argument. commonly used to convert lists to strings. Whatever string you call join on will be placed between every item in the iterable.

.lower( )

a string method that converts all letters to lowercase. Ignores numbers and symbols.

.upper( )

a string method that converts all letters to uppercase. Ignores numbers and symbols.

.popitem( )

dict method that removes and returns a random key value pair in a dict. Takes no args

dict( )

function that creates a dictionary, and assigns it to the variable you call it on. Keys and values in the dictionary are separated by =, and pairs are separated by commas. Unlike strictly defining a dictionary with {} and k/v inside, you don't need quotes around the keys this way.


Kaugnay na mga set ng pag-aaral

Market research and analysis exam 1

View Set

5 STEPS TO A 5: 500 Questions - Population (#43-63)

View Set

Vocabulary Unit 6 (English Plus 1)

View Set

Chapter 16: Anatomy of the Heart

View Set