Python break statement
The break is a keyword in python which is used to bring the program control out of the loop.
Example
str = "python"
for i in str:
if i == 'o':
break
print(i);
Python continue Statement
The continue statement in Python is used to bring the program control to the beginning of the
loop.
Syntax
#loop statements
continue
#the code to be skipped
Example
i=0
while(i < 10):
i = i+1
if(i == 5):
continue
print(i)
Output
1
2
3
4
6
7
8
9
10
Unlike every other programming language we have used before, Python does not have a
switch or case statement. To get around this fact, we use dictionary mapping.
The end parameter
The end parameter is used to append any string at the end of the output of the print statement
in python.
By default, the print method ends with a newline. This means there is no need to explicitly
specify the parameter end as '\n'. Programmers with a background in C/C++ may find this
interesting.
Let us look at how changing the value of the end parameter changes the contents of the print
statement onscreen.
The below example demonstrates that passing '\n' or not specifying the end parameter both
yield the same result. Execute 2 lines of code at a time to see the result.
print("Studytonight",)
print("is awesome")
print("Studytonight", end= "\n")
print("is awesome")
Output:
Studytonight
is awesome
Studytonight
is awesome
On the other hand, passing the whitespace to the end parameter indicates that the end
character has to be identified by whitespace and not a newline (which is the default).
print("BCA Dept.", end=' ')
print("is awesome")
Output:
BCA Dept. is awesome
The sep parameter
Sometimes, we may wish to print multiple values in a Python program in a readable manner.
This is when the argument sep comes to play. The arguments passed to the program can be
separated by different values. The default value for sep is whitespace. The sep parameter is
primarily used to format the strings that need to be printed on the console and add a separator
between strings to be printed. This feature was newly introduced in Python 3.x version.
The below example shows that passing sep parameter as whitespace or not passing the sep at
all doesn't make a difference. Execute every line of code to see the result.
print("Study", "tonight")
print("Study", "night", sep = ' to')
Output:
Study tonight
Study to
night
The below example shows different values that are passed to the sep parameter.
print("Study", "tonight", sep = '')
print("Study", "tonight", sep = ' & ')
Output:
Studytonight
Study & tonight
Note: The sep parameter, used in conjunction with the end parameter is generally used in
production code to print data in a readable fashion.
Python range() function
Python range() function returns the sequence of the given number between the given range.
range() is a built-in function of Python. It is used when a user needs to perform an action a
specific number of times. range() in Python(3.x) is just a renamed version of a function called
xrange in Python(2.x). The range() function is used to generate a sequence of numbers.
Python range() function for loop is commonly used hence, knowledge of same is the key
aspect when dealing with any kind of Python code. The most common use of range() function
in Python is to iterate sequence type (Python range() List, string, etc. ) with for and while
loop.
Python range() Basics
In simple terms, range() allows the user to generate a series of numbers within a given range.
Depending on how many arguments the user is passing to the function, user can decide where
that series of numbers will begin and end as well as how big the difference will be between
one number and the next. Range() takes mainly three arguments.
start: integer starting from which the sequence of integers is to be returned
stop: integer before which the sequence of integers is to be returned. The range of
integers end at stop – 1.
step: integer value which determines the increment between each integer in the
sequence
Example 1: Demonstration of Python range()
for i in range(10):
print(i, end=" ")
print()
# using range for iteration
l = [10, 20, 30, 40]
for i in range(len(l)):
print(l[i], end=" ")
print()
# performing sum of natural
# number
sum = 0
for i in range(1, 11):
sum = sum + i
print("Sum of first 10 natural number :", sum)
Output :
0123456789
10 20 30 40
Sum of first 10 natural number : 55
There are three ways you can call range() :
range(stop) takes one argument.
range(start, stop) takes two arguments.
range(start, stop, step) takes three arguments.
range(stop)
When user call range() with one argument, user will get a series of numbers that starts at 0 and
includes every whole number up to, but not including, the number that user have provided as
the stop.
Pass Statement
The pass statement is a null operation since nothing happens when it is executed. It is used in
the cases where a statement is syntactically needed but we don't want to use any executable
statement at its place.
For example, it can be used while overriding a parent class method in the subclass but don't
want to give its specific implementation in the subclass.
Consider the following example.
Example
list = [1,2,3,4,5]
flag = 0
for i in list:
print("Current element:",i,end=" ");
if i==3:
pass
print("\nWe are inside pass block\n");
flag = 1
if flag==1:
print("\nCame out of pass\n");
flag=0
Program to print half pyramid using *
*
**
***
****
*****
Source Code
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print("* ", end="")
print("\n") or print()
Program to print half pyramid a using numbers
1
12
123
1234
12345
Source Code
rows = int(input("Enter number of rows: "))
for i in range(rows):
for j in range(i+1):
print(j+1, end=" ")
print("\n")
Program to print half pyramid using alphabets
A
BB
CCC
DDDD
EEEEE
Source Code
rows = int(input("Enter number of rows: "))
ascii_value = 65
for i in range(rows):
for j in range(i+1):
alphabet = chr(ascii_value)
print(alphabet, end=" ")
ascii_value += 1
print("\n")
Inverted half pyramid using *
*****
****
***
**
*
Source Code
rows = int(input("Enter number of rows: "))
for i in range(rows, 0, -1):
for j in range(0, i):
print("* ", end=" ")
print("\n")
Inverted half pyramid using numbers
12345
1234
123
12
1
Source Code
rows = int(input("Enter number of rows: "))
for i in range(rows, 0, -1):
for j in range(1, i+1):
print(j, end=" ")
print("\n")
Program to print full pyramid using *
*
***
*****
*******
*********
Source Code
rows = int(input("Enter number of rows: "))
k=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(end=" ")
while k!=(2*i-1):
print("* ", end="")
k += 1
k=0
print()
Full Pyramid of Numbers
1
232
34543
4567654
567898765
Source Code
rows = int(input("Enter number of rows: "))
k=0
count=0
count1=0
for i in range(1, rows+1):
for space in range(1, (rows-i)+1):
print(" ", end="")
count+=1
while k!=((2*i)-1):
if count<=rows-1:
print(i+k, end=" ")
count+=1
else:
count1+=1
print(i+k-(2*count1), end=" ")
k += 1
count1 = count = k = 0
print()
Python Program to Find HCF or GCD
# Python Program to find GCD of Two Numbers using While loop
num1 = int(input("Enter 1st number: "))
num2 = int(input("Enter 2nd number: "))
i=1
while(i <= num1 and i <= num2):
if(num1 % i == 0 and num2 % i == 0):
gcd = i
i=i+1
print("GCD is", gcd)
Another type
# Python Program to find GCD of Two Numbers a temp variable
num1 = int(input())
num2 = int(input())
a = num1
b = num2
while(num2 != 0):
# swap using temp variable
temp = num2
num2 = num1 % num2
num1 = temp
gcd = num1
print(gcd)
Python Program to Check Leap Year using If Statement
year = int(input("Enter a year: "))
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
Python Program to Check Prime Number
num = int(input("Enter a number: "))
if num > 1:
for i in range(2,num):
if (num % i) == 0:
print(num,"is not a prime number")
print(i,"times",num//i,"is",num)
break
else:
print(num,"is a prime number")
else:
print(num,"is not a prime number")
Wap for following shape
1
1 2 1
1 2 3 2 1
n = int(input("Enter Diamond Pattern Rows = "))
for i in range(n):
for j in range(n-i+1):
print(end=" ")
for k in range(i+1):
print(k+1, end="")
h=i
for p in range(i,0,-1):
print(h, end="")
h=h-1
print()
Try yourself ….
Write a Python Program to Print Multiplication Table
Write a Python Program to find Sum of N Natural Numbers
Python Fibonacci Series program
Python Program to find LCM of Two Numbers
Python Program to find Prime Factors of a Number