XbiteGaming
XbiteGaming

Reputation: 3

What does [0] of an input variable achieve?

y = [0, 0, 0, 0, 0, 0]

while True:
 x = input()
 if x[0] == "A":
 y[0] += int(x[2:])

Could someone explain to me what this code means? X isn't a list, right? So how do you do [0] of it?

Upvotes: 0

Views: 857

Answers (4)

Grismar
Grismar

Reputation: 31354

input() asks the user for text input. Text is returned as and stored in a variable with a data format (type) called a string, which is just a string of characters.

A string (str) is indexable:

s = '12345'
print(s[2:])  # prints 345

In general, [x:y:s] is indexing something with a 'slice'. x is the start, y is the end and s is the step size. Note that the start is included, but the end is not, so it's "starting at x, up to y, in steps of s". And indexing in Python, like most languages, starts at 0, not at 1.

For example:

print(s[0])     # prints 1
print(s[1::2])  # prints 24
print(s[:2])    # prints 12
print(s[::2])   # prints 135

Many data types are indexable with numbers and slices: lists, tuples, strings, arrays, DataFrames, etc. Some data types allow for indexing with other types as well, for example the dictionary.

Upvotes: 3

anirudh sharma
anirudh sharma

Reputation: 143

You can think of string in Python as a list of characters/alphabet

>>> site = "stackoverflow"
>>> for index, character in enumerate(site):
...     print(f"Position of '{character}' in site is: {index}")
...
Position of 's' in site is: 0
Position of 't' in site is: 1
Position of 'a' in site is: 2
Position of 'c' in site is: 3
Position of 'k' in site is: 4
Position of 'o' in site is: 5
Position of 'v' in site is: 6
Position of 'e' in site is: 7
Position of 'r' in site is: 8
Position of 'f' in site is: 9
Position of 'l' in site is: 10
Position of 'o' in site is: 11
Position of 'w' in site is: 12

>>> for index, character in enumerate(site):
...     print(f"Position of '{character}' in site is: {index}")

>>> site[5:]  # taking slice of string - from index 5 to the end
'overflow'

You can refer to this for more in-depth: How To Index and Slice Strings in Python 3

Explanation of your code:

y = [0, 0, 0, 0, 0, 0]

while True:
    x = input()  # say, user inputs A2123
    if x[0] == "A":  # x[0] is the character value at 0th index which is "A"
        # x[2:] is slice of x from 2nd index to end, i.e. "123" (a string value).
        # So int("123") will covert it into 123 (an integer value)
        y[0] += int(x[2:])

Upvotes: 0

iLittleWizard
iLittleWizard

Reputation: 415

x = input()

According to the python docs, the input() function returns a string:

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example:

Therefore, the value of x is the user's input.

So how do you do [0] of it?

x[0] return the first character (at the index 0) of x.

if x[0] == "A":
    y[0] += int(x[2:])

This code will check if the first character (at the index 0) of x is "a", if yes, the first element of y (y[0]) will be added by the value of the integer value of x[2:], which is the string after the second character of x.

Upvotes: 2

ljdyer
ljdyer

Reputation: 2086

To elaborate on the comment above, 'strings are indexable' means that accessing the 0th element of a str will give you the first character of the string (and 1st element the second letter, and so on).

So in the program above, if the user enter enters 'Aardvark' the if statement will evaluate to True, but if they enter 'Bear' it will evaluate to False.

the if statement will throw an IndexError only if the string is empty (i.e. the user pressed Enter without typing anything).

Upvotes: 0

Related Questions