Say "Hello" to the world in Python
# Try your first Python outputprint('Hello, Python!')After executing the cell above, you should see that Python prints Hello, Python!
. Congratulations on running your first Python code!
There are two popular versions of the Python programming language in use today: Python 2 and Python 3. The Python community has decided to move on from Python 2 to Python 3, and many popular libraries have announced that they will no longer support Python 2.Since Python 3 is the future, in this course we will be using it exclusively. How do we know that our notebook is executed by a Python 3 runtime? We can look in the top-right hand corner of this notebook and see "Python 3".We can also ask directly Python and obtain a detailed answer. Try executing the following code:
import sysprint(sys.version)
3.6.7 | packaged by conda-forge | (default, Nov 6 2019, 16:19:42) [GCC 7.3.0]
In addition to writing code, note that it's always a good idea to add comments to your code. It will help others understand what you were trying to accomplish (the reason why you wrote a given snippet of code). Not only does this help other people understand your code, it can also serve as a reminder to you when you come back to it weeks or months later.To write comments in Python, use the number symbol
#
before writing your comment. When you run your code, Python will ignore everything past the#
on a given line. print('Hello, Python!') # This line prints a string# print('Hi')Hello, Python!After executing the cell above, you should notice thatThis line prints a string
did not appear in the output, because it was a comment (and thus ignored by Python).The second line was also not executed becauseprint('Hi')
was preceded by the number sign (#
) as well! Since this isn't an explanatory comment from the programmer, but an actual line of code, we might say that the programmer commented out that second line of code.
Everyone makes mistakes. For many types of mistakes, Python will tell you that you have made a mistake by giving you an error message. It is important to read error messages carefully to really understand where you made a mistake and how you may go about correcting it.
For example, if you spell
frint
, Python will display an error message. Give it a try:# Print string as error messagefrint("Hello, Python!")
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-4-313a1769a8a5> in <module> 1 # Print string as error message 2 ----> 3 frint("Hello, Python!") NameError: name 'frint' is not defined
The error message tells you:After executing the cell above, you should notice thatThis line prints a string
did not appear in the output, because it was a comment (and thus ignored by Python).The second line was also not executed becauseprint('Hi')
was preceded by the number sign (#
) as well! Since this isn't an explanatory comment from the programmer, but an actual line of code, we might say that the programmer commented out that second line of code.
No comments:
Post a Comment