Keywords
Keywords are not the same as built-in functions. Keywords are reserved by Python and cannot be used as names (such as variable names), overwritten or redefined. A full list is found by going onto the Python command line and:>>> import keywordand include the following which I have already mentioned in this blog:
>>> print (keyword.kwlist)
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
>>>
- break - to get out of loops early
- elif - an alternative in if structures
- else - used in if structures
- False - one of two possible Boolean values
- for - for loops go through a range of numbers or a sequence of items one at a time
- if - if structures make decisions based on whether something (such as a comparison) is true or not
- in - both used in for loops and for searching lists
- True - one of two possible Boolean values
- while - used to create while loops that loop around while a condition or comparison is true
Built-in Functions
Built-in functions are always available in Python - you don't need to import them. It is also a bad idea to use them as names/identifiers. Technically you can override them and assign them as variable names or redefine their function, but it is a really bad idea, for example:>>> print = 'Hello World'And thus we have messed up the print() function for this command line session.
>>> print (print)
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
print (print)
TypeError: 'str' object is not callable
>>>
A proper list of built-in functions for Python 3.4 can be found here:
https://docs.python.org/3.4/library/functions.html
These include some which I have already mentioned:
- close() - closes a file
- float() - tries to convert a string or integer to a floating point number
- input() - for taking user-typed information from the command line and using it
- int() - tries to convert a string or float to an integer
- len() - gives the length (number of elements) of whatever collection is in the brackets
- list() - tries to convert whatever is in the brackets into a list
- open() - tries to open a file
- print() - outputs whatever is printed onto the command line
- range() - gives a range of numbers, typically used in a for loop.
- str() - tries to convert a floating point or integer to a string
Actually, going through the page given in the link, I've discovered a number of potentially very useful functions. I hope to make use of them in later posts.
No comments:
Post a Comment