L3 Cooment
L3 Cooment
L3 Cooment
• Creating a Comment
Comments starts with a #, and Python will ignore them:
Example:
#This is a comment
print("Hello, World!")
• Comments can be placed at the end of a line, and Python will ignore the rest of
the line:
Example
print("Hello, World!") #This is a comment
• Multi Line Comments
Python does not really have a syntax for multi line comments.To add a
multiline comment you could insert a # for each line:
Example:
#This is a comment
#written in
#more than just one line
print("Hello, World!")
• Since Python will ignore string literals that are not assigned to a
variable, you can add a multiline string (triple quotes) in your code,
and place your comment inside it:
Example
"""
This is a comment
written in
more than just one line
"""
print("Hello, World!")
Python Keywords
Create a main.py:
import constant
print(constant.PI)
print(constant.GRAVITY)
Output
3.14 9.8
In the above program, we create a constant.py module file.
Then, we assign the constant value to PI and GRAVITY. After
that, we create a main.py file and import
the constant module. Finally, we print the constant value.
Variable Names
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume). Rules for
Python variables:
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive (age, Age and AGE
are three different variables)
Assigning values to Multiple Variables
x = "awesome"
print("Python is " + x)
Output: Python is awesome
• You can also use the + character to add a
variable to another variable:
x = "Python is "
y = "awesome"
z= x+y
print(z)
Output: Python is awesome
• For numbers, the + character works as a
mathematical operator:
x=5
y = 10
print(x + y)
Output: 15
• If you try to combine a string and a number,
Python will give you an error:
x=5
y = "John"
print(x + y)
Global Variables
Global Variables
• Variables that are created outside of a function (as in all of the examples above)
are known as global variables.
• Global variables can be used by everyone, both inside of functions and outside.
Example
Create a variable outside of a function, and use it inside the function
x = "awesome"
def myfunc():
print("Python is " + x)
myfunc()
Example
Create a variable inside a function, with the same name as the global variable
x = "awesome"
def myfunc():
x = "fantastic"
print("Python is " + x)
myfunc()
print("Python is " + x)
Output:
Python is fantastic
Python is awesome
The global Keyword
Normally, when you create a variable inside a function, that variable is local,
and can only be used inside that function.
To create a global variable inside a function, you can use the global keyword.
Example
• If you use the global keyword, the variable belongs to the global scope:
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)
• Also, use the global keyword if you want to change a global variable inside
a function.
Example
• To change the value of a global variable inside a function, refer to the
variable by using the global keyword:
x = "awesome"
def myfunc():
global x
x = "fantastic"
myfunc()
print("Python is " + x)