0% found this document useful (0 votes)
60 views

Python Reviewer

The document provides examples of Python code for common string, list, date, and random number operations. It includes examples of formatting strings, getting substrings, replacing characters, sorting lists, generating random integers and floats, and performing date calculations like getting the weekday from a date object. It also includes a sample code that calculates an employee's gross pay based on their daily rate, work schedule, and birthday.

Uploaded by

asasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

Python Reviewer

The document provides examples of Python code for common string, list, date, and random number operations. It includes examples of formatting strings, getting substrings, replacing characters, sorting lists, generating random integers and floats, and performing date calculations like getting the weekday from a date object. It also includes a sample code that calculates an employee's gross pay based on their daily rate, work schedule, and birthday.

Uploaded by

asasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

FLOAT GET SINGLE CHARACTER

x = float(3) x = "Computer"
print(x) print(x[1])

STRING GET CHARACTER INDEX

a = "Hello" s1="banana"
print(a) print (s1.find("an"))

INPUT GET CHARACTER BY RANGE

print('Enter your name:') x = "Computer"


x = input() print(x[0:4])
print('Hello, ' + x)

REPLACE CHARACTER
NEXT LINE PYTHON
s = 'abc12321cba'
print('YOU \nARE \nGREAT') print(s.replace('a', ''))

CONCATENATE ARRAY

a = "Hello" cars = ["Ford", "Volvo", "BMW"]


b = "World"
c = a + " " + b
print(c) WHILE LOOP

# Take user input


number = 2
MULTIPLYING STRING
# Condition of the while loop
x = 'Boom ' while number < 5 :
print(x*3) print("Thank you")
# Increment the value of
OUTPUT: the variable "number by 1"
Boom Boom Boom number = number+1
FOR LOOP NUMBER Generates a random floating-
point number in the interval
for x in range(6): [0,1)
print(x)
uniform(x, y)
It generates a floating-point
value between x and y.
FOR LOOP ARRAY
sample(population, k)
fruits = ["apple", "banana", Selects k unique random
"cherry"] elements from a population
for x in fruits: sequence or set.
print(x)
choice(seq)
STRING UPPER, LOWER, TITLE CASE Chooses a random element from a
non-empty sequence seq.
name='i Love python'
shuffle(x)
#boolean Shuffles list x in place.
print(name.isupper())
print(name.islower()) seed(x)
Generates the same sequence of
#convert random numbers every time you
print(name.upper()) call seed(x).
print(name.lower())
print(name.title())
ROUND UP OFF DOWN

RANDOMIZER import math

import random The built-in round() function


rounds values up and down.
print(random.randint(0,9))
The math.floor() function
randint(x, y) rounds down to the next full
Generates a random integer from integer.
x to y, including the x and y.
The math.ceil() function rounds
randrange(start, stop, step) up to the next full integer.
Generates a random integer in
the range(start, stop, step)

random()
FORMATTING #get weekday

newval = "{:,.2f}".format(val) bday =


newval ="{:05,.2f}".format(val) datetime.datetime(day=25, month
= 10, year =2021)
SORT print(bday.weekday())

cars=[“Ford”,”Volvo”, “BMW”]
cars.sort()
cars.sort(reverse=True) #VALUE
EQUIVALENT----------------
#monday = 0
DATE #tuesday = 1
#wednesday = 2
import datetime #thursday = 3
#friday =4
now = datetime.datetime.now() #sat = 5
now = datetime.datetime.today() #sun = 6

bday=datetime.datetime(day=1,mo
nth=1,year=2021) variable.strftime("%d/%m/%Y")

#get details
print(now.day) CONVERTING DATES
print(now.month)
print(now.year) from datetime import datetime
print(now.time)
print(now.hour) date_string = "02/12/2021"
print(now.minute)
print(now.rewind) print("date_string =",
date_string)
#min max
datetime.time.min date_object =
datetime.time.max datetime.strptime(date_string,
datetime.date.min "%d/%m/%Y")
datetime.date.max #date_object =
datetime.datetime(day=int(date_
print(datetime.date.resolution) string[2:4]),
print(datetime.datetime.resolut month=int(date_string[0:1]),
ion) year=int(date_string[5:9]))
date_object = SAMPLE CODECHUM CODE
date_object.strftime("%B %d ,
%Y") import math
import datetime
#%m = 12 (month)
#%B = 12 to December (month) sched=input('Working Days: ')
#%b = 12 to Dec (month) rate=float(input('Daily Rate:
'))
print("date_object =", bday=input('Birthday: ')
date_object) start=input('Starting Date: ')
end=input('Ending Date: ')

VAT
#deduct days
Vatable Sales = Total Sales/ dayA =
1.12. datetime.datetime(day=int(start
VAT = Vatable Sales x 1.12. [3:5]),month=int(start[0:2]),
Total Sales = Vatable Sales + year=int(start[6:10]))
VAT. dayB =
datetime.datetime(day=int(end[3
:5]),month=int(end[0:2]),
MONTHS & DAYS year=int(end[6:10]))

1. January – 31 days days = dayB - dayA


days = str(days)
2. February – 28 days in a
common year and 29 days in i=0;
leap years (2024) newday=''
3. March – 31 days
while(days[i]!=' '):
4. April – 30 days newday=newday+str(days[i])
5. May – 31 days i=i+1

6. June – 30 days
7. July – 31 days days = int(newday)
8. August – 31 days
9. September – 30 days #get month
10. October – 31 days x = int(bday[0:2])
y = int(start[0:2])
11. November – 30 days
z = int(end[0:2])
12. December – 31 days
#days divide (1 week equals to
3 days)
dateval = math.ceil(days / 7)
dateval = dateval * 3

#ifsame month
if(x==y or x==z):
x = int(bday[3:5])

b =
datetime.datetime(day=int(bday[
3:5]),month=int(bday[0:2]),
year=int(end[6:10]))
if((b >= dayA) and (b <=
dayB) ):
newrate =
(((rate*2) * dateval) + rate)

else:
b =
datetime.datetime(day=int(bday[
3:5]),month=int(bday[0:2]),
year=int(start[6:10]))
if((b >= dayA) and (b
<= dayB) ):
newrate =
(((rate*2) * dateval) + rate)
else:
newrate =
(((rate*2) * dateval))
else:
newrate = (((rate*2) *
dateval))

n = "{:,.2f}".format(newrate)

print('\nGross Pay: '+ str(n))

You might also like