Python - Other

Reverse a String

s = input("enter a string : ")

# reverse a given string
def palindrome(string):
    x = ""
    for i in string:
        x = i + x
    return x

print(palindrome(s))

Try Except

try:
    value = 10 / 0
    number = int(input("Enter a number: "))
    print(number)
except ZeroDivisionError as err:
    print(err)
except ValueError:
    print("Invalid Input.")
except Exception as e:
    print("Unknown error : " + str(e) + ".")

//Output : division by zero


Tuples

coordinates = [(4, 5), (6, 7), (80, 34)]
coordinates[0] = (1, 2, 3)

print(coordinates[0])
//Output : (1, 2, 3)

Use CMD trigger pip install to install the third party module

Downloaded files will save in external library > Python > Lib > site-packages.


Install Command

install pip -> pip install packagesName


Uninstall Command

uninstall pip -> pip uninstall packagesName

Variables & Data Types

//Text
character_name = "John"

//Numeric
character_age = 50.5678213

//Boolean
is_male = True

print("There once was a man named " + character_name + ", ")
//Output : There once was a man named John,
print("he was " + str(character_age) + " years old. ")
//Output : he was 50.5678213 years old.

character_name = "Mike"

print("He really liked the name " + character_name + ", ")
//Output : He really liked the name Mike,
print("but didn't like being " + str(character_age) + ".")
//Output : but didn't like being 50.5678213.

While Loop

i = 1

while i <= 10:
  print(i)
  i += 1

print("Done with loop")
//Output : 1
//         2
//         3
//         4
//         5
//         6
//         7
//         8
//         9
//         10
//         Done with loop

Working With Numbers

print(2)
//Output : 2
print(2.0987)
//Output : 2.0987
print(-2.0987)
//Output : -2.0987
print(3 + 4.5)
//Output : 7.5
print(3 - 4.5)
//Output : -1.5
print(3 * 4.5)
//Output : 13.5
print(3 / 4.5)
//Output : 0.6666666666666666
print(3 * 4 + 5)
//Output : 17
print(3 * (4 + 5))
//Output : 27
print(10 % 3)
//Output : 1


my_num = 5

print(my_num)
//Output : 5
print(str(my_num) + " is my favourite number.")
//Output : 5 is my favourite number.
print(-my_num)
//Output : -5
print(abs(-my_num))
//Output : 5
print(pow(3, 2))
//Output : 9
print(pow(4, 6))
//Output : 4096
print(max(4, 6))
//Output : 6
print(min(4, 6))
//Output : 4
print(round(3.2))
//Output : 3
print(round(3.7))
//Output : 4


from math import *

print(floor(3.2))
//Output : 3
print(floor(3.7))
//Output : 3
print(ceil(3.2))
//Output : 4
print(ceil(3.7))
//Output : 4
print(sqrt(36))
//Output : 6.0
print(sqrt(31))
//Output : 5.5677643628300215

Working With Strings

print("Giraffe\nAcademy")
//Output : Giraffe
//         Academy
print("Giraffe\"Academy")
//Output : Giraffe"Academy

phrase = "Giraffe Academy"

print(phrase.lower())
//Output : giraffe academy
print("phrase.upper())
//Output : GIRAFFE ACADEMY
print(str(phrase.isupper()))
//Output : False
print(str(phrase.upper().isupper()))
//Output : True
print(str(len(phrase)))
//Output : 15
print(phrase[0])
//Output : G
print(str(phrase.index("G")))
//Output : 0
print(phrase.replace("Giraffe", "Elephant"))
//Output : Elephant Academy

Writing to Files

employees.txt (before function call)

 


employee_file = open("../employees1.txt", "a")

employee_file.write("Toby - Human Resources")
employee_file.write("\nKelly - Customer Service")

employee_file.close()


employees.txt (after function call)

Toby - Human Resources
Kelly - Customer Service