#๐ import
315 messages ยท Page 1 of 1 (latest)
@sullen imp
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.
hey
hey
should we start from the previous question first?
yes
if __name__ == '__main__':
main()
double underscore
oh
if name == 'gay':
main()
lets unpack a few things first
i asked if you knew how imports in python work in detail
so lets look at that first, because it will be important to explain the double underscore if check above
import mypackage
here you have this import in your own file.py
i wanna be intermediate
and you have some other code in mypackage.py
what do you think happends when you write import mypacakge ?
when you import a file, all of the code inside that file will run
calculator =
print(eval(input())
is that part clear?
ok.. you have to calm down
dont you remember what i said when you first asked?
you keep jumping to other things
i heard eval can run any code
we are not talking about eval
you have too calm down and dont jump around
this will not be a quick explaination
just explain, not waste time on this
im trying but you keep introducing new things all the time..
sudo fr ..... and eval .. and other things while we are talking
you are the one wasting time
so, what was the last thing i talked about, and what did you understand from it?
correct.. the key part is all the lines will run
yes
keep that in mind, that will be important in a short while
a module, or python file, knoows its own name
so when you do import mypackage you can check its name
import mypackage
print(mypackage.__name__)
yes test it with a module that python has math
!e
import math
print(math.__name__)
with name
:white_check_mark: Your 3.14 eval job has completed with return code 0.
math
we use it to write code that only executes based on its name
ooh
lets say you have some code that should only work if you import the math module
how to do that
you can check that you have imported the math module by its name
ill show you
import math
if math.__name__ == 'math':
print('yay you have imported math')
we can do this, but we often dont do this in this way, we do the inverse
in my example we just ignore else
the opposite
oh
instead of checking if we have imported math, we check if we are running math
the oposite of importing a file is to run the file
py file.py
this is how you would run a python file
in vscode
you can also run it by clicking your green play button in vscode
the play button just runs py file.py in the background
python is made to know the difference between a file you have run and a file you have imported
and it know the difference between the two based on the module name
the file you run, will be renamed
import random
if random.name == 'random':
print('yay') ?
it will be renamed to __main__
yes
then i can call my code that uses random
well, i said you do the inverse of this
and at end do else that will just ignore if not imported
you dont really check if an imported name is equal to itself,
you check the oposite, ytou check if the file has been run or if the file has been imported
and i said, the file you run, will be renamed
!e
print(__name__)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
__main__
if you check if file run or imported and it will rename file
the file you run will always be named __main__
you can test it yourself
hmm
make a.py and b.py and have print(__name__) in both
if i do this i'll rename file to main?
run them each at a time
not rename file, rename the files __name__
and the name will be __main__
NAME = 'eivl'
AGE = '43'
def greet(name, age):
print(f'Hello {name}, you are {age} years old')
greet(NAME, AGE)
put this code in greet.py
you need two files for this greet.py and main.py
let me know whhen you have done these two things
you 43?
let me know when you have done these two things
ok
your main.py file is empty.. lets add some code to it
your greet.py file has the code above right?
from greet import greet
greet(name='sorncss', age=16)
age is picked randomly arbitary
๐
i want you to run main.py and see what happends
show me what happends i mean
Unexpected identifier 'main'
SyntaxError
24.10.2025, 09:50:22
so i have to do that as function
show what you have done please
pasted code u said
share a screenshot of what you have done please
show me greet.py
i dont know what editor this is though
the files needs to be saved and be in the same locatioon
acode
i just pasted same code
it looks absolutely same
just one space at line 1
because of ```py
must not start with a space
(but a blank line is fine)
yes, the issue is you have an space at the start
ok
i can run it on my machine and show you if you like to see the example i wrote
notice a thing here @sullen imp
when i run main.py prints out my greet and your greet
that is because, when you import something, all lines of the file is run
but we dont want that
we want greet.py to behave one way when i run it, and another way when i import it
when i import the code, i dont want to run the greet() function
withh me on this?
Because eivl is keeping all their code in a git repo. Ignore it - it is not relevant.
you have done something incorrect, lets look at my screen
there no function called greet
yes there is. a named my function greet
you send me code without that bro
i did! ๐
the code i wrote works, want me to show you?
You can copy/paste eivl's greet.py from here: #1431166143883640934 message
i dont want you to jump all around
we already talked about this
and you said i could explain, but your not letting me do that
you can run code here on discord
!e
NAME = 'eivl'
AGE = '43'
def greet(name, age):
print(f'Hello {name}, you are {age} years old')
greet(NAME, AGE)
:white_check_mark: Your 3.14 eval job has completed with return code 0.
Hello eivl, you are 43 years old
bruh, ig my IDE just sucks
i can show you instead
and what happends when you import something?
yes
if from filename import functon then just function
if you defined it
and same folder
just function is imported, but all lines are run no matter what
from greet import greet
greet(name='sorncss', age=16)
how many times are greet function run when you do these?
So that's all about imports?
it runs two times, do you agree?
only one more thing
yes
now i will add one more line to fix this issue of it priting two times
because u call function which u called previously and define own age and name
it will fix the issue of code running all lines when importing
yes
i want to guard agains that code getting run when i import
but still import it when i run the file
how??
ohhh
now i can from greet import greet without that extra greet function beeing run
you see? pretty nice
remember what i said earlier, python renames __name__ to __main__ for the python file you run
class is a way to write code in a special way called object oriented
it is something you will learn more about in the future
match name:
case "John"
print('lol')
case _:
print('who?')
just be carefull of where you learn about it. people have a lot of strange oppininons about how to teach classes to people
match case is not class
do you wonder about match case ?
ik
ik it
i watched about it in cs50
gimme example code soi save somewhere
not sure if my correct
pattern matching is something you should practise and learn, it is very useful.. you cant learn it from watching, so you have to do the work yourself
match name:
case ('John')
print("hello, john")
case _:
print("Who?")
this?
this video is a fun take on interpreted vs compiled language
follow form more programming content specially in javascript , python , c++ , java and become skillful coder and developer
Don't forget to LIKE, COMMENT, and SUBSCRIBE for more tech comedy and insightful developer content!
#programming #coding #DeveloperLife#WebDevelopment #UIUXDesig...
i would not worry about pattern matching until you have learned the fundamentals of python
i can explain what it is used for if you want, but i will not give out examples. those examples will confuse you. you have to have a solid foundation first
ok
have you use lists, tuples and dicts before?
do you know what they are?
then im not able to explain pattern matching clearly
you should focus on learning the basics and fundamentals of python
the book you where recommended multiple times, it goes over all these
then you will not learn programming
ok
you have to read and write
watching someone else do the work will not give you the option to learn it
at best, you will only recognise it
and you cant learn programming quickly
@delicate field
#python #tutorial #course
Python list set tuple tutorial example explained
00:00:00 intro
00:00:49 list
00:09:40 set
00:12:40 tuple
00:14:22 conclusion
List = [] ordered and changeable. Duplicates OK
Set = {} unordered and immutable, but Add/Remove OK. NO duplicates
Tuple = () ordered and unchangeable. Duplicates OK. FASTER
thats why those 15 hours videos are silly
yes?
there is no way for me to explain advanced concepts to you if you have not spend the required time to learn about the fundamentals
you cant learn thiis from watching a video
#python #tutorial #course
dictionary = a collection of {key:value} pairs
ordered and changeable. No duplicates
capitals = {"USA": "Washington D.C.",
"India": "New Delhi",
"China": "Beijing",
"Russia": "Moscow"}
print(dir(capitals))
print(help(capitals)...
bro
so take the recommendation you have been given and do the work
i need to read a book in big 2025
when i can watch hundreds of guides, and try code myself
so like lists is justvaluee, strings inside a [
depends on how much work you do now, but for an average student studding programming in schoool. 2-3 years
you should stay away from youtube to learn programming,
you should do what we have recommended you to do.
but we are not here to force you, only give out advice and help! ๐
i will not read books
no
then have a great day and good luck! ๐
:warning: The owner of this post is no longer in the server.
!close
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.