DEV Community

Kuhanraja A R
Kuhanraja A R

Posted on

day - 5,6,7,8 at payilagam "small task" "return statements" "code resuability "os module, sys module"

Mini calculator:


print("Simple Calculator")
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Choose operation (+, -, *, /): ")

if op == '+':
    print("Result:", a + b)
elif op == '-':
    print("Result:", a - b)
elif op == '*':
    print("Result:", a * b)
elif op == '/':
    print("Result:", round(a / b,2))
else:
    print("Invalid operation")
Enter fullscreen mode Exit fullscreen mode

Length Checker:

name = input("Enter your name: ")
print("Your name has", len(name), "characters.")
Enter fullscreen mode Exit fullscreen mode

Password length check:

name = input("enter your password: ")
password = len(name)

if password <=8:
    print("your password is short")
if password >=10:
    print("your password is long")
if password == 9:
    print("your password is valid")
Enter fullscreen mode Exit fullscreen mode

Return statements :

A return statement is used to end the execution of the function call and it “returns” the value of the expression following the return keyword to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. A return statement is overall used to invoke a function so that the passed statements can be executed.

def chitchat():
    print("chatting with friends:)

def withdraw():
    print("Entering",pin)
    return 500

def watch():
    return CSK

chitchat()
purse = withdraw(1234)
print(purse)
Enter fullscreen mode Exit fullscreen mode
def calculate_amount(unit):
    if unit<100:
       print('FREE')

    elif unit<200:
         print(unit*1.20)

    elif unit<300:
         print(unit*2)

consumed_units = eb_reading()
calculate_amount(consumed_units)
Enter fullscreen mode Exit fullscreen mode

In python everything is an object.

In python every python is a module.

code reusability, Documentation string, name of file.


import calc2
calc2.add(123,321)

print(calc2.__doc__)
print(dir(calc2))
print(calc2.__name__)
print(calc2.__file__)
print(calc2.__spec__)
print(calc2.package)
Enter fullscreen mode Exit fullscreen mode

Otp generator:


import random:
print (random.__doc__)
print(dir(random))
print(round(round.random(),2)*100)
print(dir(random))
print(random.randint(100000,10000000)
Enter fullscreen mode Exit fullscreen mode

import calc2 import add, subtract
add(10,20)
Enter fullscreen mode Exit fullscreen mode

from emojize import emojize
print(emojize(:thumbs_up:"))
Enter fullscreen mode Exit fullscreen mode
import sys
print(sys.argv)
print(sys.__doc__)

python3 user.py mathan salman kuhan

print(sys.argv[1])

import sys
print(sys.path)
print(type(sys.path))
sys.path.append('folder')

import calc2
calc2.add(10,20)
Enter fullscreen mode Exit fullscreen mode

OS module :

import os
print(os.getcwd())
os.mkdir("abcd")
os.chdir('home/muthu/documents/B10')
os.rmdir('home/muthu/documents/abcd')
print(os.listdir('home/muthu/Documents/B10)
os.makedirs('home/muthu/Documents/B10/Salman/mathan/kuhan')
os.removedirs('home/muthu/Documents/B10/Salman/mathan/kuhan')
os.rename('home/muthu/documents/abcd','home/muthu/documents/pqrs')

import os
print(os.__doc__)
print(os.utime('home/muthu/documents/B10/calc2.py')
print(help(os.time))
print(dir(os))

import os
print(os.path)

popen() --> function

import os
print(os.system("Python3 --version")

import os
print(os.listdir())
print(os.path.exists('home/muthu/documents/B10')
print(os.path.exists('os.py')

import os
print(os.path.getsize("calc.py"))

import os
print(os.cpucount())
print(os.getlogin())
Enter fullscreen mode Exit fullscreen mode

Sys module:

import sys
sys.version
sys.version_info
sys.stdout
sys.stdin
sys.floatinfo.
sys.intinfo
sys.copyright

import keyword
keyword.__doc__
keyword.kwlist
len(keyword.kwlist)

help('import')
help('False')

ESC + shift + : + q --> exiting one terminal

import sys
print(sys.executable)
print(sys.platform)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)