Python for beginners/some vocabulary: Difference between revisions
From Algolit
Line 14: | Line 14: | ||
− | * '''CLASSES''' contain '''METHODS''', a series of FUNCTIONS that | + | * '''CLASSES''' contain '''METHODS''', a series of FUNCTIONS that belong to an object |
− | The string-module contains classes with functions that allow you to calculate the length of the | + | The string-module contains classes with functions that allow you to calculate the length of the string, slice etc |
* Anything in Python can be an '''OBJECT''', because it can be assigned to a variable or can be an argument (input) for a function - see also OBJECT ORIENTED PROGRAMMING | * Anything in Python can be an '''OBJECT''', because it can be assigned to a variable or can be an argument (input) for a function - see also OBJECT ORIENTED PROGRAMMING | ||
Line 22: | Line 22: | ||
* An '''ATTRIBUTE''' is is a way to move from one object to the other | * An '''ATTRIBUTE''' is is a way to move from one object to the other | ||
− | “Apply the power of the almighty dot” - objectname.attributename – and magic! | + | “Apply the power of the almighty dot” - objectname.method(attributename) – and magic! |
− | Note: to know the | + | Note: to know the methods of an object: dir() |
− | + | dir() prints the list of attributes | |
− | |||
− | dir() | ||
>>> sentence = "This is so exciting!" | >>> sentence = "This is so exciting!" |
Latest revision as of 17:11, 8 December 2015
Other pages: Python for Beginners // Loops and Conditions // Create anthology
- A string or string object is a MODULE in Python
- A MODULE is a file, which contains Python statements.
Normally among these there are:
- definition statements - the execution of the module defines some functions
- class statements - the execution of the module defines some classes.
A module may also contain names (variables) and other directly executable Python statements.
- you can write your own definitions/functions and save them I a file that you can import into another python-file
- such a document is an external module, called at the beginning of a script using 'import name_module'
- CLASSES contain METHODS, a series of FUNCTIONS that belong to an object
The string-module contains classes with functions that allow you to calculate the length of the string, slice etc
- Anything in Python can be an OBJECT, because it can be assigned to a variable or can be an argument (input) for a function - see also OBJECT ORIENTED PROGRAMMING
- An ATTRIBUTE is is a way to move from one object to the other
“Apply the power of the almighty dot” - objectname.method(attributename) – and magic!
Note: to know the methods of an object: dir()
dir() prints the list of attributes
>>> sentence = "This is so exciting!"
>>> dir(sentence)
>>> suffix = "ing!"
>>> print(sentence.endswith(suffix))
>>> len(sentence)
>>> print(sentence.endswith(suffix,20))
>>> print(sentence.endswith(suffix,16))
>>> print(sentence.endswith(suffix,16,20))
- Exercise: think of formula that gives TRUE for suffix 'is'
Note: a long string can be annotated using 3 """ a lot of words """
If you have a very long string, you better use:
very_long_string = (
"For a long time I used to go to bed early." "There was no one in the house. Etc etc …."
)