Answer to Question #332877 in Python for Hari

Question #332877

Variables, python operators,data types,string slicing, range of string using list, python conditional statements,user input, looping statement s, python function s, exception handling, file handling, module concept write a demart code using all these things


1
Expert's answer
2022-04-24T17:16:47-0400
# Variables


x = 5
print('Value of x is ' + str(x))


print('') # newline


# User input


#print('Your input is ' + input('Enter your input: ') )


print('') # newline


# Operators



print('5 + 5 is ' + str(5 + 5))
print('5 - 5 is ' + str(5 - 5))
print('5 * 5 is ' + str(5 * 5))
print('5 / 5 is ' + str(5 / 5))
print('5 % 5 is ' + str(5 % 5))
print('5 ** 5 is ' + str(5 ** 5))
print('5 // 5 is ' + str(5 // 5))


print('') # newline


# Data types


x = 5
print(f"x is {type(x)}")
x = 'hey'
print(f"x is {type(x)}")
x = 6.0
print(f"x is {type(x)}")
x = 5j
print(f"x is {type(x)}")
x = ('apple', 'potato')
print(f"x is {type(x)}")
x = ['pineapple', 'pen']
print(f"x is {type(x)}")
x = range(12)
print(f"x is {type(x)}")
x = {"banana", "cherry"}
print(f"x is {type(x)}")
x = {"name" : "Dave", "age" : 21}
print(f"x is {type(x)}")
x = frozenset({"apple", "banana", "cherry"})
print(f"x is {type(x)}")
x = False
print(f"x is {type(x)}")
x = b"Apple"
print(f"x is {type(x)}")
x = bytearray(8)
print(f"x is {type(x)}")
x = memoryview(bytes(80))
print(f"x is {type(x)}")


print('') # newline


# String slicing


s = 'pineapple'
slice = s[:4]
print(f"Original string: {s}\nSlice: {slice}")


print('') # newline


# Range of strings using list


lst = [str(i) for i in range(1, 10)]


print(lst)


print('') # newline


# Conditional statements


x = 10
y = 8


print(f"x is {x}\ny is {y}")


if x > y:
    print(f'{x} bigger than {y}')


print('') # newline


# Looping statements


print('For loop:')


for i in range(10):
    print(i)


print('While loop:')


k = 3
while k > 0:
    print(f'k is {k}')
    k -= 1


print('') # newline


# Functions


def printHelloWorld():
    print('Hello world!!!')


def printSomeText(text):
    print(f'Some text is {text}')


printHelloWorld()
printSomeText('hey')


print('') # newline


# Exception handling


def doThing():
    # doing thing
    raise NotImplementedError


try:
    doThing()
except NotImplementedError:
    print('Exception!')


print('') # newline


# File handling


file = open('test.txt', 'w')
file.write('Hello')
file.close()


f = open('test.txt', 'r')
for line in f:
    print('Line: ' + line)


print('') # newline


# Module concept


# module.py


def hey():
    print('hey')


# main.py


import module
module.hey()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS