Function argument basics, membership operators
No account is required. Learn the material, try the examples, and mark it complete when you are ready to move on. Sign in with email only if you want to save progress across devices.
By now, we have studied certain code blocks. But what if we have a situation where we have codes which needs repetition. We would have to write the same code all over again. For that purpose, we use loops.
But before we start loops, lets learn first a little basics of functions are. We have been using functions all along, right.
print()
range()
list()
set()
dict()
tuple()
were a couple that we used
Lets dive a little deeper into the range() function. functions will have a whole dedicated chapter, so for now, all I want my audience to focus on is, how we use them.
Range() function
You could recall that we used a range() function to generate lists. Actually, range() is just a sequence generator. range() is a python object (which we will cover in depth later)
So, if we make a range(10) and print its output, it'd look something like this.
range(10)range(0, 10)Its when we create a sequence from it that it generates those numbers.
For now, we will generate numbers using a list function. So, those numbers will be created inside a list.
list(range(10))[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]list(range(4))[0, 1, 2, 3]Depending on the values we pass to the range() function, it generates a sequence of numbers.
For example:
list(range(4))gives us four numbers, starting from 0:
[0, 1, 2, 3]Notice that 4 itself is not included.
By default, range() starts at 0 and stops before the number we provide.
We can also tell range() where to start and where to stop:
list(range(2, 6))gives us:
[2, 3, 4, 5]We can even specify the step size:
list(range(2, 10, 2))gives us:
[2, 4, 6, 8]So, at its simplest, range() can take:
range(start, stop, step)where:
start- where the sequence beginsstop- where the sequence ends (not included)step- how much to move forward each time
There are many more things we can do with range(), but these three ideas are enough for now.
list(range(0, 5))[0, 1, 2, 3, 4]# list(range(start_point, end_point))list(range(2, 5))[2, 3, 4]As you might have noticed, the end point value is excluded from the range.
$$list(range(2, 5))$$
gives us a range of numbers starting from 2 ending at 4 as the end point is excluded.
list(range(10, 20))[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]These numbers inside range(10, 20) separated by a comma that we enter inside the range functions are called function arguments.
Function Arguments
There could be different types of arguments depending on how a particular function works. We'll have a whole dedicated section on function arguments later. For now, let's focus on the range function and its arguments.
# list(range(start, end, step))list(range(5, 15, 1))[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]Notice the third argument that is the step argument
list(range(5, 15, 2))[5, 7, 9, 11, 13]list(range(5, 15, 3))[5, 8, 11, 14]list(range(5, 15, 4))[5, 9, 13]list(range(5, 15, 5))[5, 10]Lets increase the step counter and use a different range of numbers
list(range(0, 500, 50)) # end point 500 is excluded[0, 50, 100, 150, 200, 250, 300, 350, 400, 450]list(range(0, 501, 50))
# look at the end point to
# understand why 500 is now included in the output[0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500]list(range(0, 501, 100))[0, 100, 200, 300, 400, 500]list(range(0, 501, 200))[0, 200, 400]list(range(0, 501, 300))[0, 300]list(range(0, 501, 500))[0, 500]list(range(0, 501, 600))[0]print() function
We have been using the print function since the first class. Now, lets use the knowledge of function arguments to understand this print function.
print("Hello, world")Hello, world
The "Hello, world" string we passed inside the print function, was print function's first argument.
print by default will print any values we pass inside it. Lets print multiple values now.
variable_a = 10
variable_b = 20
string_variable = "Hello"
name = "Tickles"
print(variable_a)
print(variable_b)
print(string_variable)
print(name)10
20
Hello
Tickles
We can print all of these 4 variables with just one print function
print(variable_a, variable_b, string_variable, name)10 20 Hello Tickles
We could get creative here and print whatever we want
print("value of variable_a:", variable_a)value of variable_a: 10
print("What sorcery is this?", variable_b)What sorcery is this? 20
print("Are you a good person?", string_variable)Are you a good person? Hello
print("Whos the man? Mee...", name)Whos the man? Mee... Tickles
String Formatting
We will later learn, how to print these things in a better fashion using string formatting.
But just as an example for our beautiful curious audience, let me show how print function could be used with string formatting
Note: I'll say again, we will cover string formatting much later in the ebook.
name = "Ash"
f"My name is {name}"
fruit = "mango"
"i love to eat {}".format(fruit)'i love to eat mango'name = "Tickles"
print(f"The value of {name}")The value of Tickles
name = "Tickles"
print("The value of {}".format(name))The value of Tickles
Remember Multi-line strings?
It's okay, multi-line strings are just strings written over multiple line. Let's use them
number_of_apples = 10
audio_gear = "Sennheiser"
price = "$4"
discounted_price = "$3"
print(f"""I purchased {number_of_apples} apples
from local shop, but then I noticed a discount
on my favourite headphones {audio_gear}.
The total price was {price} but the
discounted price was a good {discounted_price}.
I went out to buy apples but
ended up buying {audio_gear} headphones.""")I purchased 10 apples
from local shop, but then I noticed a discount
on my favourite headphones Sennheiser.
The total price was $4 but the
discounted price was a good $3.
I went out to buy apples but
ended up buying Sennheiser headphones.
Membership Operators
Remember, we left the membership operators for later.
There are two membership operators in python.
They are used to check if a value exists inside a sequence.
The in keyword
1 in [1, 2, 3]TruePython says, yes the value 1 exists inside the list of elements [1, 2, 3]
value1 = 10
value110sequence_list = [1, 2, 3, 10]
sequence_list[1, 2, 3, 10]value1 in sequence_listTrue12 in sequence_listFalsePython says the value 12 does not exist inside the list.
The not in operator
Just the opposite of in operator, because we used not keyword here to reverse the output
sequence_list[1, 2, 3, 10]1 in sequence_listTrue1 not in sequence_listFalseFinished this lesson?
Keep your place here, or save it across devices with an email link.
Saved in this browser. Save progress with email →