G7 - Python - Code Reference Guide
G7 - Python - Code Reference Guide
Sr. Commands,
No Functions or Description with Example
Keywords
1. Comments with # Comments can be used to explain Python code. A comment does
not have to be text that explains the code, it can also be used to
prevent Python from executing code. Comments starts with a #,
and Python will ignore that line code.
Ex 1 :
#This is my first program
print("Hello, World!")
Ex 2:
#print("Hello, World!")
print("Cheers, Mate!")
Ex :
print("Hello World")
3. \n This adds a new line to your output with print(). If you see this in
a print() that means that the current line ends at that point and a
new line starts right after it.
Ex:
print(“Hello \nGood Morning.”)
Output :
Hello
Good Morning
Ex:
age = 5
name = "John"
print(“My name is”,name)
print(“My age is”,age)
5. input() Function The input() function allows user input and the same can be
stored in a variable.
Ex:
pet=input(“What was your first pet name?”)
print(“Your first pet was ”,pet)
6. eval() - Evaluate IT evaluates the input(in the form of string/text) and converts it
into a number or digit.
Ex:
#Addition of two numbers
no1=eval(input(“Enter no1”))
no2=eval(input(“Enter no2”))
add=no1+no2
print(“Addition of the two numbers is ”,add)
7. Arithmetic
Operators
Modulus (%) - The % symbol in Python is called the
Modulus(Mod) Operator. It returns the remainder of dividing the
left variable by the right variable. It's used to get the remainder
of a division problem.
EX :
Assume X and Y are two variables.
X = 15
Y=2
1. X + Y = 17
2. X - Y = 13
3. X * Y = 30
4. X / Y = 7.5
5. X%Y=1
Example:
txt = "HELLO EVERYONE"
x = txt.lower()
print(x)
Syntax :
Example :
a = 33
b = 200
if b > a:
print("b is greater than a")
Note :
Indentation:
Python relies on indentation (whitespace at the beginning of
a line) to define scope in the code. Other programming
languages often use curly-brackets for this purpose.
Syntax :
Example :
a = 33
b = 200
if b > a:
print("b is greater than a")
else:
print("a is greater than b")
12. if..elif..else The elif keyword is Python's way of saying "if the previous
statements conditions were not true, then try this condition".
Syntax :
if expression:
statements
elif expression:
statements
else:
Statements
Example :
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
13. Nested If (If Inside You can have if statements inside if statements, this is
If) called nested if statements.
Example:
num = eval(input(“Enter Number between 1 to 99”))
if num>0:
print("This is a Positive Number")
if num<10:
print("This is a one digit Number.")
else:
print("This is a two or more digit Number.")
else:
print(“This is a Negative Number”)
Example:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")
Example:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")
Example:
a = 200
b = 33
c = 500
if not(a > b and c > a):
print("Both conditions are False")
Loops in Python
17. While loop The while loop in Python is used to iterate/repeat over
a block of code as long as the condition is true. We
generally use this loop when we don't know the
number of times to iterate beforehand.
Syntax :
while condition:
Code statements
Example:
Assume i is a variable holding numbers then print i as
long as i is less than 6:
i=1
while i < 6:
print(i)
i=i+1
Example
Print a message once the condition in previous example
is false:
i=1
while i < 6:
print(i)
i=i+1
else:
print("i is no longer less than 6")
While loop with With the break statement/keyword we can stop the
break loop or exit from the loop even if the while condition is
true.
Example
Exit the loop when i is 3:
i=1
while i < 6:
print(i)
if i == 3:
break
i=i+1