L3 Cooment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Python Comments

• Comments can be used to explain Python code.


• Comments can be used to make the code more readable.
• Comments can be used to prevent execution when testing code.

• 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

• Keywords are the reserved words in Python.


• We cannot use a keyword as a variable name, function name or any
other identifier. They are used to define the syntax and structure of
the Python language.
• In Python, keywords are case sensitive.
• There are 33 keywords in Python 3.7. This number can vary slightly
over the course of time.
• All the keywords except True, False and None are in lowercase and
they must be written as they are. The list of all the keywords is given
below.
False await else import pass

None break except in raise

True class finally is return

and continue for lambda try

as def from nonlocal while

assert del global not with

async elif if or yield


Constants

A constant is a type of variable whose value cannot be changed.


It is helpful to think of constants as containers that hold
information which cannot be changed later.
You can think of constants as a bag to store some books which
cannot be replaced once placed inside the bag.

• Assigning value to constant in Python


In Python, constants are usually declared and assigned in a
module. Here, the module is a new file containing variables,
functions, etc which is imported to the main file. Inside the
module, constants are written in all capital letters and underscores
separating the words.
• Example : Declaring and assigning value to a constant
Create a constant.py:
PI = 3.14
GRAVITY = 9.8

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.

Note: In reality, we don't use constants in Python. Naming


them in all capital letters is a convention to separate them
from variables, however, it does not actually prevent
reassignment.
Python Variables

• Variables are containers for storing data values.


• Python has no command for declaring a variable.
A variable is created the moment you first assign a
value to it.
x=5
y = "John"
print(x)
print(y)
• Variables do not need to be declared with any
particular type, and can even change type
after they have been set.
x=4 # x is of type int
x = "Sally" # x is now of type str
print(x)
Python - Variable Names

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

• Python allows you to assign values to multiple


variables in one line:
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
Note: Make sure the number of variables
matches the number of values, or else you will
get an error.
And you can assign the same value to multiple
variables in one line:
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables

The Python print statement is often used to


output variables.
To combine both text and a variable, Python
uses the + character:

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()

Output: Python is awesome


If you create a variable with the same name inside a function, this variable will be local, and can only
be used inside the function. The global variable with the same name will remain as it was, global and
with the original value.

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)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy