#πŸ”’ How can i make variables in python?

181 messages Β· Page 1 of 1 (latest)

polar field
#

hi someone can explain it please

light burrowBOT
#

@polar field

Python help channel opened

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.

fading isle
polar field
#

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}")

sly finch
sly finch
light burrowBOT
sly finch
#

that wouldn't work if the variable Name wouldn't have stored the string as it did

polar field
#

ohhh

polar field
#

: 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?

sly finch
sly finch
#

!e

def name():
    print("Hi I am John!")

name()
light burrowBOT
sly finch
polar field
#

so

#

a class what's it

#

i know that can make a list i believe that can use it then

sly finch
#

you can also send data into a function

polar field
#

data?

sly finch
#

!e

def hi(first_name):
    print(f"Hi I am {first_name}!")

hi("Jojo")
light burrowBOT
sly finch
#

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

polar field
#

hmm ok

#

hey is right that the functions can use it on the games?

sly finch
#

classes can store information (or "data" if you so will) and also have "methods" that can do different operations on the data

polar field
sly finch
polar field
#

for example if i make a class that save a phonenumber and a name can use for several things right?

sly finch
#

print() is a function, but it comes as part of the core python language, it is a so called "built-in function"

polar field
#

python have many libraries wow

sly finch
#

so you are already using functions

sly finch
polar field
sly finch
#

one last very basic thing about functions, they can also return information that you can then later use

polar field
#

pygames for games and numpy i believe that can use it for LM?

polar field
#

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
light burrowBOT
#

Hey @polar field!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
polar field
#

chatgpt resolving that whit isdigit my code have many errors lol

sly finch
#

!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}")
light burrowBOT
polar field
#

.

#

ok

#

the function return can show a result as the print()

sly finch
#

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

polar field
#

Wait, I'll be back in a second. I really have to have dinner. You're helping me a lot, sir.

sly finch
#

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}")
light burrowBOT
sly finch
#

!gif

light burrowBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

sly finch
#

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

polar field
#

already understand it

#

wait

#

def add(x):
r = x + 4
print(f"The result is: {r}")

y = add(5)
print(y)

light burrowBOT
#

Hey @polar field!

Please edit your message to use a code block

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
polar field
#

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

sly finch
#

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)

sly finch
#

!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)
light burrowBOT
sly finch
#

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

polar field
#

so The program is not doing what I'm asking for.

#

ok

#

so y = add(5) have a string

#

?

sly finch
#

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)

sly finch
#

computers are very good at doing what they are told to do
software bugs are almost always the fault of a human

polar field
#

ok i get it a little bit

sly finch
#

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

polar field
#

what's parameters in the function for example x is a parameters ?

sly finch
#

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

polar field
#

It's the same as with the code that sends none, you could also do more things outside of a function.

sly finch
#

!e

def add(x, y):
    result = x + y
    return result

total = add(5, 4)
print(f"The total is: {total}")
light burrowBOT
sly finch
#

if we would just have printed it from within the function we couldn't have done the following

polar field
sly finch
#

!e

def add(x, y):
    return x + y

num = add(5, 4)
grand_total = add(num, 7)
print(f"The total is: {grand_total}")
light burrowBOT
sly finch
#

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)
light burrowBOT
sly finch
#

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)

polar field
#

ok

#

I think the libraries that people make are functions that we have done, okay?

sly finch
#

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

sly finch
#

they can call C and C++ libraries (and libraries made in other languages too) for example

polar field
#

C?

#

what is C ohhh IS THE FATHER OF PYTHON right?

sly finch
#

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

polar field
sly finch
#

a lot of major games are written in C++ or C# for example

#

C, C++ and C# harder to learn then Python

polar field
#

oh.

sly finch
#

i would recommend starting with Python first, and when you know it you can look at other languages if you still like to

latent delta
#

I think python is its own thing

sly finch
#

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

latent delta
#

Then there's the java, c, and c++ family

#

But it is a good place to start

polar field
#

oh

#

i will use python but thanks

sly finch
#

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)

polar field
#

oop?/

sly finch
#

OOP = Object Oriented Programming
python has that too

#

when you use classes you are in the realm of OOP

polar field
#

ok

sly finch
#

so, if you are [a little bit more] comfortable with functions now, we can start to talk a little bit about classes

polar field
#

sure i toke note about all of functions friend

#

just i need practice it

sly finch
#

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?

polar field
#

sure

sly finch
#

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)

polar field
#

yeah how 2453535355

#

i don't know about numbers on us

#

a

sly finch
#

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)

polar field
#

and *?

#

ths sorry

#

bruh

fading isle
sly finch
#

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

sly finch
polar field
#

yeah him help me

sly finch
# fading isle <@936769916072259654> did you already explain why "variables are containers" is ...

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

polar field
#

alright thanks @sly finch we can be friends?

sly finch
#

@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}")
light burrowBOT
sly finch
#

@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()
light burrowBOT
sly finch
#

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) πŸ‘‹

polar field
light burrowBOT
#
Python help channel closed for inactivity

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.