0

Hey, I have previously programmed in C, PHP & Javascript and I have recently started out with Python but the syntax of the functions is quite different and confusing? Like if we call len(abc) we enter the param inside the function unlike the function abc.upper() where we use enter the param using a dot. Its kinda confusing?

4
Contributors
5
Replies
57
Views
3 Years
Discussion Span
Last Post by snippsat
Featured Replies
0

The dot operator is used to acces methodes and variables that are part of a class. It's saying from class abc i'm using the upper function. This function still takes it's parameters in between the brackets. You are just saying that you want to use the upper function from the abc class and not some random other upper() function.

1
>>> 'abc'.upper()
'ABC'
Comments
yes, but why do we enter the argument using the dot. Why not inside the brackets?
0

if we want to convert 'xyz' to uppercase, why dont we enter the argument inside the function. I means 'xyz' string is not a class?

0

a string is an object of the class String. which is why we can use the function on it.

0

if we want to convert 'xyz' to uppercase, why dont we enter the argument inside the function

Because uppercase() is a method that belong to string class and only works on strings.
The Built-in Functions work on many class/objects.
Let's see how len() work on string and list.

>>> s = 'hello'
>>> len(s)
5

>>> lst = [1,2,3]
>>> len(lst)
3

Both string and list has methods that belong only to string and list class.
Look at append() for list.

>>> lst = [1,2,3]
>>> lst.append(4)
>>> lst
[1, 2, 3, 4]

append() work only for list,so no point that it should built in function.

s = 'hello'
>>> s.append('world')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
AttributeError: 'str' object has no attribute 'append'

Use dir(str) or dir(list) to see methods that belong to string and list class.

Edited by snippsat

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.