Author: SUNNY BHAVEEN CHANDRA
For more information -
[1] Python notes- https://c17hawke.github.io/Python/
[2] Python YouTube Playlist- https://youtube.com/playlist?list=PLrdaCCBhU_hnxIzB7EJlY-pfYOMGRycAK
print("Hello, world")
Hello, world
The print()
function is a very useful tool in Python that allows you to display information to the user. It’s often used to output text or other data to the screen.
To use the print()
function, you simply type print()
and then put whatever you want to display inside the parentheses.
It’s a built-in function, which means that it’s always available for use without having any other dependencies.
print(4 + 2)
6
print("Greetings from Sunny!")
Greetings from Sunny!
Comments in python 💬¶
# This is a comment, can be served as inline documentation for python code snippets
print("Hello, world")
Hello, world
# This is a comment, can be served as inline documentation for python code snippets
# print("Hello, world") # No ouput will be seen as this code is commented out
multiline comments -
"""This is a comment, can be served as
inline documentation for python code snippets"""
print("Hello, world")
Hello, world
'''The following code is simple a
mathematical calculation'''
print("Sum of 4 and 2=", 4+2)
print("Product of 4 and 2=", 4*2)
Sum of 4 and 2= 6 Product of 4 and 2= 8
Errors / Exception in python ❌¶
When something goes wrong in your Python code, an error or exception occurs.
- An error is a mistake in the code that prevents the program from running correctly.
- An exception, on the other hand, is an event that occurs during the execution of a program that disrupts the normal flow of the program's instructions.
We will learn about them by observing throughout the course
# Example of Syntax error
# observe the error message here
print("hello, world"
Cell In[8], line 3 print("hello, world" ^ SyntaxError: unexpected EOF while parsing
# The following code will give Syntax error
print("Product of 4 and 2=", 4*2
Cell In[9], line 2 print("Product of 4 and 2=", 4*2 ^ SyntaxError: unexpected EOF while parsing
# Correction user parenthesis
print("Product of 4 and 2=", 4*2)
Product of 4 and 2= 8
# The following code will give exception Zero Division Error
print("Division of 4 and 0=", 4/0)
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[11], line 2 1 # The following code will give exception Zero Division Error ----> 2 print("Division of 4 and 0=", 4/0) ZeroDivisionError: division by zero
OBSERVE: The difference between Error and Exception in the following examples¶
Error ❌¶
print("Sum of 4 and 2=", 4+2)
print("Product of 4 and 2=", 4*2)
# The following code will give Syntax error
print("Product of 4 and 2=", 4*2
# The above error will not allow to run the entire program
Cell In[12], line 7 # The above error will not allow to run the entire program ^ SyntaxError: unexpected EOF while parsing
Exception ⚠️¶
print("Sum of 4 and 2=", 4+2)
print("Product of 4 and 2=", 4*2)
# The following code will give exception Zero Division Error
print("Division of 4 and 0=", 4/0)
# The above error will only throw an exception at the last line.
# It will now stop printing the 1st two lines because
# The code is correct Syntax wise!
Sum of 4 and 2= 6 Product of 4 and 2= 8
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) Cell In[13], line 5 2 print("Product of 4 and 2=", 4*2) 4 # The following code will give exception Zero Division Error ----> 5 print("Division of 4 and 0=", 4/0) 7 # The above error will only throw an exception at the last line. 8 # It will now stop printing the 1st two lines because 9 # The code is correct Syntax wise! ZeroDivisionError: division by zero
Explore about the above error messages in chatGPT or Bing Chat 💬¶
in order to get quick undersatanding about them if you get stuck later while writing your own program -
An example shown below -¶
BingChat - https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx