Python Unconditional Statements And String Operations✍️

In the last session, we discussed python loops. I hope you all understood them well. Now, we're learning about unconditional statements and string operations.

Let’s start,

Unconditional Statements

Python has various types of loop functions like for and while. These loops are automatic and efficiently repeats the tasks. But sometimes, where you want to exit the loop completely, skip an iteration or ignore that condition. These can be done by unstructured loop control statements.

  • Break
  • Continue
  • Pass

Break Statements

The break statement terminates the loop.

Break statements using letters

Input:

for i in 'python':
    if i=='h':
        break
    print("Letter: ",i)

Output:

Letter: p
Letter: y
Letter: t

Break statements using numbers

Input:

number = 10                   
while number > 0:              
   print ('Current Number :', number)
   number = number -1
   if number == 5:
      break

print ("exit")

Output:

Current Number : 10
Current Number : 9
Current Number : 8
Current Number : 7
Current Number : 6
exit

Continue Statements

 The loop to skip the remainder of its body and immediately retest its condition prior to reiterate.

Continue statements using letters

Input:

for i in 'python':
   if i=='h':
      continue
   print("letter:",i)

Output:

letter: p
letter: y
letter: t
letter: o
letter: n

Continue statements using the range

Input:

for i in range(5):
  if i == 3:
    continue
  print(i)

Output:

0
1
2
4

Pass Statements

  • Pass statement is a null operation.
  • Nothing happens when it executes.

Pass statements using letters

Input:

for i in 'python':
   if i=='h':
      pass
      print("pass block")
   print("letter:",i)

Output:

letter: p
letter: y
letter: t
pass block
letter: h
letter: o
letter: n

 Pass statements using a list

Input:

list =['a', 'b', 'c', 'd']

for i in list:
   if(i =='c'):
      pass
   else:
      print(i)

Output:

a
b
d

STRING OPERATIONS

String - sequence of characters

Different String printing options

str = 'Hello World'

# Prints complete string
print (str)

# Prints first character of the string
print (str[0])

# Prints characters starting
# from 3rd to 5th
print (str[2:5])

# Prints string starting
# from 3rd character
print (str[2:])

# Prints string two times
print (str * 2)

# Prints concatenated string
print (str + " program")

Output:
Hello World
H
llo
llo World
Hello WorldHello World
Hello World program

String Concatenation

Input:

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

Output:
HelloWorld

String function using the format

Input:

age = 24
txt = "My name is sri, and I am {}"
print(txt.format(age))

Output:
My name is sri, and I am 24

Data Types overview

type – Identifying

Input:

x = 10
print(type(x))

x = 10.5
print(type(x))

x = 'python'
print(type(x))

x = "python"
print(type(x))

Output:
<class 'int'>
<class 'float'>
<class 'str'>
<class 'str'>

Different Assigning methods 

x = y = z = "python"

print(x)
print(y)
print(z)

x = "the best language"
print("Python is " + x)

x = "Python is "
y = "the best language"
z = x + y
print(z)

OUTPUT:
python
python
python
Python is the best language
Python is the best language

Random Number generator 1 - 10

Input:

import random
print(random.randrange(1, 10))

Output:
1st Run:
5
2nd Run:
2
3rd Run:
4

Sorted Command

Input:

#purpose of sorted command
word = input("enter any word : ")
print(sorted(word))

Output:
1st Run
enter any word : lkiju
['i', 'j', 'k', 'l', 'u']
2nd Run
enter any word : iHjasbvfc
['H', 'a', 'b', 'c', 'f', 'i', 'j', 's', 'v']

Now, we have learned unconditional statements and string operations. I have provided multiple examples and various methods. In the next article, we will discuss the def function.