The Python Oracle

Create a function with four parameters

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands

--

Chapters
00:00 Create A Function With Four Parameters
02:27 Answer 1 Score 2
02:57 Answer 2 Score 6
04:36 Answer 3 Score 0
05:44 Accepted Answer Score 8
09:04 Thank you

--

Full question
https://stackoverflow.com/questions/1894...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #string #function #parameters #boolean

#avk47



ACCEPTED ANSWER

Score 8


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.




ANSWER 2

Score 6


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.




ANSWER 3

Score 2


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".




ANSWER 4

Score 0


In Python, we don't have any type for variables and what to speak of data type Python doesn't even have variables. In Python, by variable we mean a name referring to a particular value.

Example:

my_var=4

my_var is a name, and it is referring to 4. It is int in the above statement but in next,

my_var="hello"

it is a string. Now 4 is not referred to by anyone. "hello" is the value that my_var is referring.

After this, you can easily infer that data type doesn't matter for Python as long as your operations on the referring values are correct. It's all about the value, not about the name.

For your question,

>>> student_data("Brian",32,"1234567",False)
[1234567,Brian,32,False]

consider this:

def student_data(name,age student_no):
    age = age + 4       # Valid if the age is referring to number but invalid if age is string.
    age = str(age)      # If you want your number to be string then use str() function.
    age = age + "test"  # It is valid because age is referring to string now.
    return "your string"

Also check this demo: http://ideone.com/pYSl3l

The article Python Basics: Understanding The Variables explains the same in pictorial way.