#πŸ”’ printing the wrong thing..?

65 messages Β· Page 1 of 1 (latest)

past quartzBOT
#

@magic aurora

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.

magic aurora
#

im a beginner btw

#
fruits = ["apple", "banana", "orange"]
print(fruits)

print(fruits[1])```
#

hmm making it 0 seems to work

fallow swallow
#

yeah 0 is the first index

zenith ember
#

yeah, in programming you count from 0 up

magic aurora
#

idk in lua its 1

fallow swallow
#

0, 1,2, 3

#

orange would be index 2

magic aurora
#

luas index are better
cuz 0 is the placeholder for nothing
basically nil

fallow swallow
#

i dont know lua

bleak frigate
#

But this is python, and we have something similar. None

fallow swallow
#

but most of the time they count by 0

magic aurora
#

:o

sturdy cosmos
#

0 is because it's the offset from the first element

#

so array[0] means offset 0 from first item which means first item

magic aurora
#

so it calculates the offset to first item instead of counting the items

sturdy cosmos
#

It was because of how C designed it from a pointer

bleak frigate
#

-1 wraps it around to the end of the list bc of this as well

magic aurora
#

i hope i get used to this cuz i cant use lua if i want to get a job later

sturdy cosmos
#

Most programming languages use this offset system as well, so I'd say getting used to it is better in the longer term

winged crescent
#

I think over 70% of the programming languages start with 0 because of how binary works. Correct me if I'm wrong πŸ˜…

fallow swallow
#

yeah I would say that

magic aurora
#

do they use none instead of nil too

fallow swallow
#

in python None I would say is equal to nil

sharp crescent
#

nil and None are essentially the same, yes

fallow swallow
#

in python you also dont need to specify the type of a variable

#

has its disadvantage but also an advantage

magic aurora
#

thats a downside for me

#

i like writing something before the variables it makes it easier to read imo

bleak frigate
#

You can specify the type, it’s just not going to throw an error if you change its type

#

Var: int = 20

sharp crescent
#

type annotations are primarily for IDEs, they will yell at you if you type things wrong

sturdy cosmos
#

There are tools to help you with it, pylint is famous for checking typehinting for you

#

There are also tricks to check during runtime, but generally it's not worth the effort - it's more of a design problem

#

Python has a huge advantage and that's its development speed

#

Not having to define static type is a boon and a problem at the same time, but if you pick python for the speed of development, it's a boon

fallow swallow
#

yeah of course I say it because in other programming language maybe you need char a = ... long number = .....

magic aurora
#

luaify

fallow swallow
#

the interpreter does define the types I guess

sturdy cosmos
#

The interperter will "guess" the type based on what you do, for example

number_str = input(...)
number = int(number_str)  # interpreter will guess this is an int```
#

The type will be defined much later when it's being translated

fallow swallow
#

ok yeah thats why many people say python is very slow from what I heard

magic aurora
#

what if the user inputs a string

fallow swallow
#

input returns a string

magic aurora
#

it says int(number_str) tho

#

isnt integer whole numbers

sturdy cosmos
#

It will raise a ValueError if you call int() on a string that's not convertible

fallow swallow
#

input returns a string

#

you can check that on ide

sturdy cosmos
fallow swallow
#

--> str

sturdy cosmos
#

For better or worse, if it comes to that you might have picked the wrong tool for the job

#

But there are always ways to improve performance, I'll worry about it least

sturdy cosmos
fallow swallow
#

yeah of course python good for beginners cause C is really close to assembly

sturdy cosmos
#

python is a high level programming language, which means you do not have to worry about low level concepts like managing pointers / memory usage/address

#

For beginners these concepts are very hard to understand at first

fallow swallow
#

yeah thats it but keep in mind high needs to translate to low

wanton moat
#

The reason indexes start from 0 is because it's actually a memory offset.

#

so you're doing memory_location + (index * object_size)

past quartzBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.

#

πŸ”’ printing the wrong thing..?