Exercise 2

Caps and lowercase printer

user_string = input ("Enter a string:")

print (user_string.upper())
print (user_string.lower())

# Enter a string : I love python 
#output: I LOVE PYTHON 
#output: i love python

Number operator

number_one = float (input ("Enter first number:"))
number_two = float (input ("Enter second number:"))

print ("Addition", number_one + number_two)
print ("Subtraction", number_one - number_two)
print ("Multiplication", number_one * number_two)
print ("Division", number_one / number_two)
print ( "Power", number_one ** number_two)
#Enter first number: 5
#Enter second number: 2
#Addition : 7.0
#Subtraction : 3.0
#Multiplication : 10.0
#Division : 2.5
#Power : 25.0

Why indexes begin at zero in Programming

Indexes in programming often begin with zero due to efficiency and simplicity in computation. This zero-based indexing aligns directly with memory addresses, making the calculation of an element's address more straightforward. It stems from early computing practices where memory efficiency was crucial. Additionally, zero-based indexing simplifies mathematical operations, particularly in pointer arithmetic and range representation, which is why it's commonly used in many programming languages.

What are other methods that can be used on strings?

Replace - a part of string stored inside a variable can be replaced by first coding the variables name and the instruction replace()

birthday = "my birthday is on Saturday"

new_birthday = birthday.replace ("Saturday ", "Friday ")

print (new_birthday)
#Output : my birthday is on Friday

Split - split()

It is used to split a string into a list of substrings based on a specified delimiter.

list_of_names = "Sarah Millicent Barbara Sofia"
names = list_of_names.split()
print (names)

#You can also split based on comma as the separator 
list_of_names = " Sarah Millicent Barbara Sofia"
names = list_of_names.split(",")
print (names)

# You can split based on the maximum numbers of splits to be performed 
list_of_names = "Sarah Millicent Barbara Sofia"
names = list_of_names.split(" ",2)
print (names)

Join - join()

Joins elements of an iterable into a single string using a specified separator .

sing = ('I', 'love', 'to', 'sing')
sing_too = ' '.join(sing)
print (sing_too)
#output : 'I love to sing'

Startswith- startswith()

Checks if a string starts with a specified substring

birthday = "my birthday is on Saturday"
new_birthday = birthday.startswith("my")
print (new_birthday) 
#output : True

Endswith- endswith()

Checks if a string ends with a specified substring

birthday = "my birthday is on Saturday"
new_birthday = Birthday.endswith("my")
print (new_birthday)
#output : False

Find - find()

Searches for the first occurrence of a substring in a string and returns its index

birthday = "my birthday is on Saturday"
index = birthday.find("birthday")
print (index)
#output :3

Count - count()

Count returns the number of occurrences of a substring in a string.

birthday = "my birthday is on Saturday"
count = Birthday.count ("a")
print (count)
#output : 3

What are other methods that can be used on numbers?

Abs - abs()

Returns the absolute (positive) value of a number.

number = -200
absolute_value = abs(number)
print(absolute_value)

# Output: 200

Pow - pow()

Raises a number to a specified power.

number = 2
power = 3
result = pow(number, power)
print(result)
# Output: 8

Divmod - divmod()

Returns the quotient and remainder of the division of two numbers.

quotient, remainder = divmod(10, 3)
print(quotient, remainder)
# Output: 3 1

Round - round()

Rounds a floating-point number to a specified number of decimal places or to the nearest whole number.

number = 3.7
rounded = round(number)
print(rounded)
# Output: 4
rounded_to_one_decimal_place = round(number, 1)
print(rounded_to_one_decimal_place)
# Output: 3.7

Math.sqrt - math.sqrt()

Returns the square root of a number (requires import math).

import math
number = 16
square_root = math.sqrt(number)
print(square_root)
# Output: 4.0

Math.floor -math.floor()

Returns the largest integer less than or equal to a number (rounds down).

import math
number = 3.7
rounded_down = math.floor(number)
print(rounded_down)
# Output: 3

math.ceil()

Returns the smallest integer greater than or equal to a number (rounds up).

import math
number = 3.2
rounded_up = math.ceil(number)
print(rounded_up)
# Output: 4

math.isqrt()

Returns the integer square root of a number (requires import math).

import math
number = 16
integer_square_root = math.isqrt(number)
print(integer_square_root)
# Output: 4