Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function with four parameters

Right, so I'm doing a homework assignment and I am asked to do the following:

Create a function called student data, that takes four parameters, a name (a string), age (an integer), student number (a string) and whether they are enrolled in CSCA08 (a boolean), and returns a string containing that information in the following format: [student number,name,age,enrolled].

Your code should work as follows:

>>> student_data("Brian",32,"1234567",False)
`[1234567,Brian,32,False]'
>>> student_data("Nick",97,"0000001",True)
`[0000001,Nick,97,True]'

What I came up with was this:

def student_data(name, age, student_number):
    return  '[' + student_number + ',' + name + ',' + str(age) + ']'

and when entering it into Python:

student_data("Jeremy", 19, "999923329")
'[999923329,Jeremy,19]'

(Note I left out the last bit about booleans - I'll get to that in a second.)

From what I understand, 'Jeremy' and '999923329' are strings which were both subsequently returned as part of the string in the second line. For 'age' since there were no quotations when I called the function student_data, it was interpreted as an int by Python. Then, I converted that int value into a string, so I could end up with '[999923329,Jeremy,19]'.

So technically, I guess what I'm asking is: is the parameter 'age' considered an int by python, until the return function changes it into an str type? Note that the assignment wants four parameters, two strings (which I have), one int (which I don't know if it's actually interpreted as an int) and a boolean, which leads to the following:

I really have no idea how Booleans work. Specifically, in the context of the assignment, what exactly am I supposed to do? What would an example be? I fiddled around with my code for a bit, and I came up with this:

def student_data(name, age, student_number, boolean):
    return  '[' + student_number + ',' + name + ',' + str(age) + "," + str(boolean) + ']'

And entering it in Python:

student_data("Jeremy", 19, "999923329", True)
'[999923329,Jeremy,19,True]'

That actually follows exactly what the assignment wanted me to do, but I don't like it because I don't really understand what's going on. Like, 'boolean' is a parameter that the function student_data needs to work. But what is a parameter exactly? Is it the same thing as a variable? When I enter 'True' in the python shell, what exactly is happening?

Is what is happening the same thing that happens when you assign a value to a variable? In that case, what is happening when I assign a value to a variable? The assignment is asking for a parameter to be a boolean, but I don't believe I entered a boolean into the code, did I?

And yes, if it isn't obvious already, I've never had a computer science class before.

like image 571
Jeremy Avatar asked Sep 22 '13 12:09

Jeremy


People also ask

How do you pass multiple parameters to a function?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

Can a function have 3 parameters?

Followed by one (monadic function) and closely by two arguments (dyadic function). Functions with three arguments (triadic function) should be avoided if possible. More than three arguments (polyadic function) are only for very specific cases and then shouldn't be used anyway.

Can function have multiple parameters?

Luckily, you can write functions that take in more than one parameter by defining as many parameters as needed, for example: def function_name(data_1, data_2):


3 Answers

In Python, there are objects and names. An object has a type, a name is just a pointer to an object. If an object doesn't have a name, you cannot access it (in fact, if there are no names pointing to an object, Python will get rid of it altogether).

In other languages, a name is usually called variable and the are declared of a specific type. I have read some books that talk about variables being a box, and the box can only take a certain type of thing, which is the value.

Python works a bit differently. Names which are the equivlant to variables don't have a type. They can point to any object and more specifically any object of any type.

So technically, I guess what I'm asking is: is the parameter 'age' considered an int by python, until the return function changes it into an str type? Note that the assignment wants 4 parameters, 2 strings (which I have), 1 int (which I don't know if it's actually interpreted as an int) and a boolean

Since Python is not strict about types; you usually speak of objects. So when you pass in 17 in a parameter to a function, you are passing in the object 17 which of the integer type.

However, age is just a name, and names can point to any type. Since you can pass in any object to the function (in other words, age can be any valid Python object), its not correct to say that parameter 'age' is considered an int. Its just a placeholder for any kind of object that is passed to the function.

In the body of your function, you are creating a string object by combining other objects together.

A string object knows what to do when you ask it to be combined with another object - any other object. Since it doesn't know what you'll try and combine with it; it has some sensible defaults and tries to convert stuff to a string.

If it cannot do this combination, it will give you an error:

>>> 'a' + 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects

In that scenario what we want to do is convert the object 2 into a string. To do that for integers, or really - any object at all, we use the str() method (as you have done correctly).

So what happens when you convert the object to a string, an entirely new object is created - which has no link to the original - and is the string representation of it. This is an entirely new object. A different thing altogether.

I really have no idea how Booleans work. specifically, in the context of the assignment, what exactly am i supposed to do?

The good thing is, once you know how one object works - all objects generally behave the same way. In Python, when you know how to get the string version of one object (by doing str()) you use the same technique on every-other-object. It is upto the object to figure out how to "convert" itself.

In other words - since you know that str() will take an object and convert it to its string representation; you can rest easy that it will work with every other object in Python.

As types are not strict in Python, it gives programmers this flexibility that allows them to rely on the object to know what needs to be done.

That's why str() works with booleans and int and floats and every other object. Later on in your studies when you start learning about classes (which are ways to create custom objects), you'll learn how to control what happens to your object when someone does a str() on it.

like image 121
Burhan Khalid Avatar answered Oct 18 '22 03:10

Burhan Khalid


A variable in Python is a label to an object. The code x = 7 puts the label x on the object in memory representing 7. This is somewhat like all variables being pointers. This means you can then use the label x in place of the number 7:

>>> y = x + 8
>>> print(y)
15
>>> x = "hello world!"
>>> print(x)
hello world!

A function is somewhat like a function in maths, it takes some inputs and returns an output.

Example:

def f(x, y, z):
    return x + y * z

In this example, x, y and z are the parameters. I can then call the function as so:

>>> print(f(1, 2, 3))
6

In this example, 1, 2 and 3 are the arguments.

Parameters are variables inside the function definition:

def f(x):
    print(x)
    return x + 7

>>> variable = f(5)
5
>>> print(variable)
12
>>> print(x)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    print(x)
NameError: name 'x' is not defined

Outside of the function, x is not defined. This is concept of variables being accessible only in certain places is called variable scope.

In your function, the parameter age is supposed to be given an argument of the type int. When it is called, it returns the object the variable age points to, converted to a string. This doesn't change that object.

You did enter a Boolean into the code, as an argument when you called the function:

student_data("Jeremy", 19, "999923329", True)
                                          ^
                                     There is the Boolean!

Python doesn't have a strict typing system (it uses duck typing), so you don't have to (indeed you can't) specify what the types of the arguments to a function should be. So you could actually call your function with a string, or an integer, or an instance of a class you defined for the parameter boolean.

like image 6
rlms Avatar answered Oct 18 '22 01:10

rlms


Python has untyped parameters. You can pass any object that has a __str__ method (strings return themselves, ints return the string representation of themselves... as do all the built in types, to my knowledge), and your function would work.

By using Python, your professor is asking you to "make a function of four arbitrary positional parameters and return a string representation of those objects in a different order".

like image 2
Tritium21 Avatar answered Oct 18 '22 01:10

Tritium21