#π How can i make variables in python?
181 messages Β· Page 1 of 1 (latest)
@polar field
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.
what have you tried so far?
for example They say it's a container for storing values. Does it really store my name? Name = "Jojo"
and i try to make fstrings also
print(f"My name is {Name}")
that would make the variable named Name store the string Jojo
!e - and that would print that text, here's a demo showing that it works
Name = "Jojo"
print(f"My name is {Name}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
My name is Jojo
that wouldn't work if the variable Name wouldn't have stored the string as it did
ohhh
thanks @sly finch i going take this note
: D
also i have another question what is the classes?
mainly the functions
for example
def name():
print("Hi i am john")
so if i call it with name(): that can print something?
right?
classes and functions are two completely different things
correct, but you have to format the code correctly
and with python formatting matters a lot as it's part of the syntax and not just to make it more readable
!e
def name():
print("Hi I am John!")
name()
:white_check_mark: Your 3.13 eval job has completed with return code 0.
Hi I am John!
yeah, as you can see above that works perfectly too
okk
so
a class what's it
i know that can make a list i believe that can use it then
to be able to understand classes properly you need to learn more about functions first
you can also send data into a function
data?
!e
def hi(first_name):
print(f"Hi I am {first_name}!")
hi("Jojo")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
Hi I am Jojo!
different kind of data, such as strings, numbers lists or any other object (which are also called "class instances")
all data types in python are actually classes
the class name for the data type behind strings is str and the data type for lists is simply named list and there are many more
classes can store information (or "data" if you so will) and also have "methods" that can do different operations on the data
okok wait i take this noted
games can be simple and they can be complex
you can make a simple game without functions
but you will at least use functions to create the game
and for more complex games you most probably want to create your own classes to store different information in
for example if i make a class that save a phonenumber and a name can use for several things right?
print() is a function, but it comes as part of the core python language, it is a so called "built-in function"
python have many libraries wow
so you are already using functions
yeah, there is a lot of libraries for python π
that is also what makes python as a programming language so powerful, diverse, useful and popular
i hear about pygames and numpy....
one last very basic thing about functions, they can also return information that you can then later use
pygames for games and numpy i believe that can use it for LM?
how?
let me check it about it i don't know how to use loops
while True:
name = input("Enter a name: ")
if name.isdigit():
print("Numbers are not allowed!\n")
continue
else:
print(f"Hello {name}")
print("Continuing...\n")
break
Hey @polar field!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
chatgpt resolving that whit isdigit my code have many errors lol
!e
def pi():
return 3.14159265359
radius = 5
circle_area = pi() * radius ** 2
print(f"The area of a circule with the radius {radius} is {circle_area}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
The area of a circule with the radius 5 is 78.53981633975
so we use the value that the function returns back to us when we call it
we can assign the value to a variable or use it in many other contexts
Wait, I'll be back in a second. I really have to have dinner. You're helping me a lot, sir.
print() doesn't really return anything, but it does take parameters so that you can send data in to it
print() is instead said to have a side-effect, and in this case it is that it prints out the data that you give it
you can combine the two concepts
one being that functions can receive data through arguments that you give it
the other is that you can receive data back from the function when it return any data
!e - this is just a little demo of that
def double(number):
return number * 2
pocket_change = 7
more_change = double(pocket_change)
print(f"If we double our pocket change of {pocket_change} it becomes {more_change}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
If we double our pocket change of 7 it becomes 14
!gif
above is a little animation of how variable and values works with functions that take arguments and return data back to the caller of the function
ohhh
already understand it
wait
def add(x):
r = x + 4
print(f"The result is: {r}")
y = add(5)
print(y)
Hey @polar field!
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
according to the image I saw
r = 5+4 so in y = add(5) i put in the r variable the sum of 5 + 4
ok @sly finch thanks
that wonβt do what you want
now add() just prints the string but doesn't return anything (well, any function that doesn't return anything will return None but that isn't really useful most of the time in such situations)
hmm
!e - lets try out your code and you'll see what it prints out
def add(x):
r = x + 4
print(f"The result is: {r}")
y = add(5)
print(y)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | The result is: 9
002 | None
y now only contains the None value/type (which is kind of a place holder for the concept of "nothing") and then you prints it
so The program is not doing what I'm asking for.
ok
so y = add(5) have a string
?
it's doing exactly what you told it to do
you just told it to do something else then you though you did and that was not what you wanted
no, that add function will just return None as you aren't returning any data from the function (not using the return keyword in the function)
oh ok
let me see
computers are very good at doing what they are told to do
software bugs are almost always the fault of a human
fair point π
ok i get it a little bit
sometimes that human isn't the developer of the program
it can be the developer of the interpreter that is running the code or compiler, or the operating system, but almost always some human
what's parameters in the function for example x is a parameters ?
can be, if you put it between the parentheses when you define the function
if you want to add two numbers together you can make a function that takes those two numbers, adds them together and returns the result so that you can use it for other things later on
oh oooo
It's the same as with the code that sends none, you could also do more things outside of a function.
!e
def add(x, y):
result = x + y
return result
total = add(5, 4)
print(f"The total is: {total}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
The total is: 9
if we would just have printed it from within the function we couldn't have done the following
ok I think I'm getting the code that does something inside, and another code that does something outside to show or do something.
!e
def add(x, y):
return x + y
num = add(5, 4)
grand_total = add(num, 7)
print(f"The total is: {grand_total}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
The total is: 16
unless the main thing that you want the function to do is to print something in a very specific way
then a special function for that would be a good choice
!e
def add(x, y):
return x + y
def display_total(value):
print(f"The total is: {value}")
total = add(5, 3)
grand_total = add(total, 7)
display_total(grand_total)
:white_check_mark: Your 3.13 eval job has completed with return code 0.
The total is: 15
as a simple example
where one function takes two arguments and returns a result, and another function that takes one argument and doesn't return anything but instead have a side-effect (in this case the side-effect is that it prints the data, but could be to write it to file or store it in a database or send it somewhere else over the internet)
god
ok
I think the libraries that people make are functions that we have done, okay?
these examples are doing much useful work, they are just to illustrate how functions can work in different ways
yeah, functions and classes (that acts as data types) of different kinds mostly
hmm
they can call C and C++ libraries (and libraries made in other languages too) for example
C and C++ is two popular system programming languages
most of the linux kernel is made in C and a lot of libraries too
the main/reference implementation of Python is named CPython, it is implemented in C
there are other implementations of Python as well
oh... i'm interest about it maybe i going to learn it then learning python
a lot of major games are written in C++ or C# for example
C, C++ and C# harder to learn then Python
oh.
i would recommend starting with Python first, and when you know it you can look at other languages if you still like to
I think python is its own thing
ok ok
to be good with C/C++ you need to know a lot of how computers really work on a low level, because you have to think about memory management and it's a lot easier to write code with really bad bugs in it
for example, C doesn't have classes and Object Oriented Programming (OOP)
that is one of the major additions in C++ to C
Java is also object oriented, but it can't run directly on the machine, it needs something called a virtual machine that runs the byte code that the java compiler produces (which isn't machine code)
oop?/
OOP = Object Oriented Programming
python has that too
when you use classes you are in the realm of OOP
ok
oh
so, if you are [a little bit more] comfortable with functions now, we can start to talk a little bit about classes
so, a class is a bit like a container that can store data in memory while your program is running and they can also have different methods that can do operations on the data they hold
yes, practice is the most important part of learning, you learn by doing
so you should practice writing code all on your own, not using AI to start with
AI is great to speed up your work when you know it really well, because you need to be able to read and understand the code fully in and out as AI makes a lot of mistakes too, someone that doesn't know how to read and write the code will not be able to tell when the generated code is wrong or does things slightly differently then what you wanted it to do
should we begin with some basics about classes?
sure
as i said before, classes are most of the time a sort of data type (amongst other things)
let's take your phone book for example (you mentioned name and number of a person quite early on when starting to talk about classes)
it doesn't matter for now, we can store it as a string to start as so that it can contain anything, since many times you can have a + in the beginning in-front of country codes or spaces and dashes in-between numbers or parentheses around part of the phone number and even # and * for special functions)
@sly finch did you already explain why "variables are containers" is a bad metaphor?
I don't want to derail
when you are dialing a number you can include # and * when dialing to access special features for the call, but that isn't important right now
no, please do
i mostly more see them as labels for data myself (pointing to something somewhere else in memory most of the time)
yeah him help me
i have quite limited time left, so i was going to gloss over some of the very basics of classes and objects, what they are on a vary high level and how they are different from each other
but if you want to dig into that you are free to "derail" this conversation π as it can probably be a good thing for them to understand as a basic concept from the beginning so that they don't learn things that are wrong
alright thanks @sly finch we can be friends?
sorry, i have exactly 0 "friends" on discord and generally don't do DMs anymore (hasn't for a few years now) and don't do voice chats (or screen shares) here on discord either
@polar field okay, a very quick mention about classes before i need to go
class Contact:
def __init__(self, name, phone_number):
self.name = name
self.phone_number = phone_number
alice = Contact("Alice Doe", "+1 555 1234")
bob = Contact("Bob Smith", "+1 555 3498")
this doesn't really do much yet, we have defined a class that is named Contact
we also define a special "method" (if we simplify it a bit, methods are more or less functions that belongs to a class)
this method has the special name __init__, it has the job of initializing and "instance" of the class
our __init__ method here takes three arguments, the first one is a bit special, that's the class instance we are currently working with, then the rest is just the arguments that we give to the class when we create it, so this class requires exactly two arguments when we create an instance of this class
in the __init__ method we take the data of each argument and attach it to "attributes" of the instance
then we create two instances of the Contact class with different data for each instance (instances can also be called "objects")
for each instance we create we assign them to a variable so that we don't lose them as soon as they are created
right now the class isn't doing much, other then holding on to the data in "attributes" and returning an object that references that data
!e - we can access the data, and even change the data
class Contact:
def __init__(self, name, phone_number):
self.name = name
self.phone_number = phone_number
alice = Contact("Alice Doe", "+1 555 1234")
bob = Contact("Bob Smith", "+1 555 3498")
print(alice.name)
print(f"Name: {alice.name}, Phone: {alice.phone_number}")
alice.phone_number = "+2 111 9876"
print(f"Name: {alice.name}, Phone: {alice.phone_number}")
print(f"Name: {bob.name}, Phone: {bob.phone_number}")
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | Alice Doe
002 | Name: Alice Doe, Phone: +1 555 1234
003 | Name: Alice Doe, Phone: +2 111 9876
004 | Name: Bob Smith, Phone: +1 555 3498
@polar field are you with me so far?
we can also define our own "instance methods" (these are the most common methods for a class)
!e
class Contact:
def __init__(self, name, phone_number):
self.name = name
self.phone_number = phone_number
def dial(self):
print(f"Dialing {self.name} ({self.phone_number})...")
alice = Contact("Alice Doe", "+1 555 1234")
bob = Contact("Bob Smith", "+1 555 3498")
bob.dial()
alice.dial()
:white_check_mark: Your 3.13 eval job has completed with return code 0.
001 | Dialing Bob Smith (+1 555 3498)...
002 | Dialing Alice Doe (+1 555 1234)...
this is just the very basics of classes and objects, but i hope the introduction of these concepts was useful to you
@polar field i need to go now, have a good day/evening/night (or what ever it is where ever you are) π
sad but i want be your friend and i need teachers as your here thanks ful sir : D you too
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.