You are on page 1of 10

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!")

2. print() Function Prints a message/output onto the screen.

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

4. Variables ● Variables are containers for storing data values.


● A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).
● Create a name that makes sense. For example, variable
name phone or phone_no makes more sense than just
ph or no.
● A variable name must start with a letter.
● A variable name cannot start with a number or a symbol.
● 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)

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

8 String Functions lower() - Converts a string into lower case


Assume txt is a variable..

Example:
txt = "HELLO EVERYONE"
x = txt.lower()
print(x)

upper() - Converts a string into upper case


txt = "hello everyone"
x = txt.upper()
print(x)

Merge variable a with variable b into variable c


Example:
a = "Hello"
b = "World"
c=a+b
print(c)

9. Comparison Assume there are two variables(a and b).


Operators
If - Else Conditional Statements
10. IF Statements

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.

11 IF- Else Statements

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".

The else keyword catches anything which isn't caught by


the preceding conditions.

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

LOGICAL OPERATORS (AND, OR,NOT)

14. and The and keyword is a logical operator, and is used to


combine two or more conditions. It returns True if both
conditions are true.

Example:
a = 200
b = 33
c = 500
if a > b and c > a:
print("Both conditions are True")

15. or The or keyword is a logical operator, and is used to


combine two or more conditions. It returns True if one
of the conditions is true.

Example:
a = 200
b = 33
c = 500
if a > b or a > c:
print("At least one of the conditions is True")

16. not By using not keyword we can change the meaning of


the expressions, moreover we can invert an expression.
It reverses the result, returns False if the result 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

Note: remember to increment i, or else the loop will


continue forever.
While loop with With the else statement we can run a block of code
else once the condition no longer is true or is false.

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

You might also like