#π need help installing functions
174 messages Β· Page 1 of 1 (latest)
@wise pike
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Where are you writing your code?
Into this web interface somewhere?
Or do you have your own setup?
I can take you through the idea of functions and their constituent concepts, if you need.
@wise pike
Idle Python
from my_functions import sub
print(sub(3))
May I see the code present in the my_functions module?
def square(x):
return x * x
def sub(x):
return a - b
I'm sorry I'm still new to Python so this might not be what you're asking but I got it form the my_functions module.
Is that the entirety of the my_functions module?
ok great. Cause I thought I did it right and then the computer said "SIKE you suck" and here we are\
What I'm currently doing is contemplating what I need to be telling you such that you can come to your own realisation of the problem.
Okay, let's start with variable assignment. You may have covered this concept, already, but let's just make sure the idea has a good foundation in your brain.
Explain to me your understanding of what a variable, specifically, is, and how you can create and use them.
ok so a variable is like a number or a word. It's part of a string that is determined to be printed.
It can operate in either an interger(a whole number without the decimal)
a float(a number with a decimal like 3.14
or a string(a sentence like "Johnny has 5 apples)
they only are outputted if I say Print(, if I don't say print then nothing comes out cause I didn't tell python "put this on paper" cause python is a numbers ssytem.
Okay, now I'll give my explanation.
A variable is a named reference to an object. It's like a signpost. It's not the object itself, but can be treated as if it were the object.
A variable pointing to a string is not the string itself.
what's a signpost?
A variable pointing to an integer object is not the integer object itself.
A road sign.
ah
a vairable pointing to an interger. So like it's meaning like words inside the parenthesis of interger?
It's not the book itself, but it tells you where to find the book.
We use variables to remember objects for later use. For example.
!e py my_int = 123 print(my_int)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
123
What here are the variables?
123?
is it my_int?
That's one.
is the other one =?
That's an operator.
print?
ok solid solid.
Those are the two variables in this code. my_int and print
In order to use the objects to which they refer, those names have to exist.
so variables are words
Or you have to be referring to the object more directly as a literal.
Variables are named references to objects. Yes.
Now, look at your my_functions module again.
Where has a been created?
wait I'm confused when you said object I thought those were numbers only, are objects only words?
Where has b been created?
Okay, I'll drop back to talking about objects.
Python is known as an object oriented programming language.
Objects are packages of data that float around in memory.
They are data plus functionality for interacting with that data.
Like computers and keyboards.
Different objects, different buttons you can push and knobs you can twiddle and talk to and listen to the result of those interactions.
so they're both numbers and words so long as they're in the print( parenthesis?
Each object has a type to which is belongs. A class.
The class describes the data structure, what data and functionality can and does exist in objects of its type.
For example.
objects of type str, strings, have str as their type.
I have a string
I have another string
They're both objects of type str
(Might be handy to highlight names like a and b in a distinct font.)
But they are different strings
Then there are objects of type int
1
2
123
I can have lists
[1, 2, 3]
so they don't need to be in the print parentesis to be classified as objects?
This is an object of type list.
Correct.
When they're in the print parentheses, it would be more apt to describe them as arguments.
But as a literal or as a variable or as an expression to be evaluated, they're all still objects.
wait as arguments? so then how do I know which ones are the objects if they're oohhh wait if I say Int, is that an object?
Almost everything you're interacting with in Python is in some way an object. Only the syntax itself does not count as objects.
syntax? Which button is that on the keyboard?
Syntax describes the structure of the language as you write it.
huh?
no
It shouldn't, because it's syntactically incorrect.
sorry I dunno what syntactically means?
Grammar?
ah.
So when we have variables, they are references to these data structures we call objects.
and can be used as a proxy to those objects
a reminder of how to find them
!e py print(123) # print 123 immediately var = 'abc' # remember 'abc' as var print(var) # almost like writing print('abc')
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | 123
002 | abc
when you say proxy do you mean a stand in?
Yes.
A stand in for every purpose other than reassignment of the variable, taking the signpost and pointing it at a different object.
!e py var = 123 print(var) var = 'abc' print(var)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | 123
002 | abc
ok alright so how does this help me with the sub and the square thing I'm trying to get installed on my idle?
In order to use these variables, these names, they first have to have been assigned to an object.
Have a look in your my_functions module.
What's missing?
ok I'm looking and so far I am not sure.
You try to use a and b...
Is there an a and a b anywhere else?
Where are they created?
so far no, why would there be an a or b anywhere else? wouldn't that be like throwing car engines around the city without the car so the engine is useless?
!e py print(a)
:x: Your 3.13 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File [35m"/home/main.py"[0m, line [35m1[0m, in [35m<module>[0m
003 | print([1;31ma[0m)
004 | [1;31m^[0m
005 | [1;35mNameError[0m: [35mname 'a' is not defined[0m
!e py a = 123 print(a)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
123
If you haven't told Python what a and b refer to, how can you expect it to do anything with a and b in the function?
You've given the function an x parameter.
But you don't use it.
because this guy did somehow
Okay, let's talk a little about functions.
oooooooohhhhhhhh shit I think I get what you mean.
Can you look closely at how that guy defined sub, and used sub? There are some differences between his and your code, aren't there?
the x I put on sub(x)
!e ```py
def my_function():
print('Hello, world.')
my_function()
my_function()
my_function()```
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | Hello, world.
002 | Hello, world.
003 | Hello, world.
Here, I have created a function. It has no parameters (the names you write in the def line parentheses) and its sole purpose is to print the text, Hello, world.
a lot. Unfortunately I'm blind as a bat cause everyone knows the solution but me. And keep in mind I never used python before and I'm still learning but so far my learning progress has a lot of road blocks and language barriers to the point where I wanna pull my hair out and throw a car at the guy who thought it was funny to make coding this confusing
There is a variable assignment of my_function to the function object I have created.
Well, can you point out the differences?
ok that I get but how do I do the square(3) thing withouth having to write the def line every single time?
No I can't that's why I came here.
Alright.
I'd like to talk about functions. π
When you say
def sub(x):
return a - b
... where do you expect a and b to come from? What exactly should the code do when you tell it to a - b, given that you haven't told it what a and b are?
yes please
So here, I create the function object, give it a name, then I call it, cause it to run, three times.
We see that it runs the print three times.
Functions can also accept things called arguments. Arguments are objects given in the call. I will demonstrate.
do you have to type it again in order for it to work?
!e ```py
def my_function(parameter):
print(parameter)
my_function('I am an argument.')```
:white_check_mark: Your 3.13 eval job has completed with return code 0.
I am an argument.
Here, we see that the string given in the call, 'I am an argument' is the argument. It has the name parameter assigned to it within the context of the execution of the function.
parameter = 'I am an argument.'```
As if I were doing this.
But inside the function.
We can also give functions multiple parameters.
!e ```py
def my_function(a, b):
print(a)
print(b)
my_function('apples', 'bananas')```
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | apples
002 | bananas
Here, a and b are the parameters/variables.
'apples' and 'bananas' being the arguments.
If you need, you can even substitute variables for the literals.
!e ```py
def func(obj):
print(obj)
var = 123
func(var)```
:white_check_mark: Your 3.13 eval job has completed with return code 0.
123
See how this works?
Parameters are like a function's inbox.
The stuff you feed the parameters are the arguments.
Functions can have a lot of different parameters, and different ways of describing how they are to be provided. This is just the simple, positional way.
I'm sorry I know you mean well I'm just having a tough time comprehending this information. You've helped me understand some concepts but I'm not as advanced in knowledge as you are.
So a lot of this is going over my head.
Sorry I'm a bum I'm not as smart as most people on this server.
This feeling is normal.
Don't worry about it. π
Everyone feels it when they're starting off.
thanks for understanding, I just picked up python a few months ago and it's not a straight shot like i was hoping, there's so many twists and turns that I think I fell on my head several times
Shall I continue, would you like to take a break, or would you like to terminate the session?
Gimmie a sec I need a break
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.