|
| 1 | +fromdatetimeimportdatetime,timedelta |
| 2 | +fromcoloramaimportFore |
| 3 | + |
| 4 | +# variables |
| 5 | +firstName='Saurabh' |
| 6 | +lastName='Dixit' |
| 7 | +fullName='Hello {0} {1} '.format(firstName,lastName) |
| 8 | +currentDate=datetime.now() |
| 9 | +oneDay=timedelta(days=1) |
| 10 | +oneWeek=timedelta(weeks=1) |
| 11 | +yesterday=currentDate-oneDay |
| 12 | +previousWeekDays=currentDate-oneWeek |
| 13 | +date1="26/05/2020 15:26:00"# Date value as string |
| 14 | +x=40 |
| 15 | +y=0 |
| 16 | + |
| 17 | +# Datetime variables print |
| 18 | +print(currentDate) |
| 19 | +print(Fore.RED+"Today's date is: ",currentDate) |
| 20 | +print(Fore.BLUE+"Today's day is: ",currentDate.day) |
| 21 | +print(Fore.GREEN+"Month is: ",currentDate.month) |
| 22 | +print("Year is: ",currentDate.year) |
| 23 | +print("Hours is: ",currentDate.hour) |
| 24 | +print("Second is: ",currentDate.minute) |
| 25 | +print(" ") |
| 26 | +print(datetime.strptime(date1,"%d/%m/%Y %H:%M:%S"))# String date value converted into Datetime format |
| 27 | +print(" ") |
| 28 | +print("Yesterday's Date was: ",yesterday) |
| 29 | +print("Previous Weekday was: ",previousWeekDays) |
| 30 | +print("") |
| 31 | + |
| 32 | +# Capitalise the first letter of string |
| 33 | +print(Fore.YELLOW+"Hello"+" "+firstName.capitalize()+' '+lastName.capitalize()) |
| 34 | +# upper case of enitre string |
| 35 | +print(Fore.WHITE+"Hello"+" "+firstName.upper()+' '+lastName.upper()) |
| 36 | +# Lower case of entire string |
| 37 | +print("Hello"+" "+firstName.lower()+' '+lastName.lower()) |
| 38 | +print(fullName)# Print variable |
| 39 | + |
| 40 | +# Exception Handling |
| 41 | +try: |
| 42 | +print(x/y) |
| 43 | +exceptExceptionase: |
| 44 | +print(Fore.RED+"Error No 1 is: "+str(e))# will be executed when error occurs |
| 45 | +finally: |
| 46 | +print() |
| 47 | +print(Fore.WHITE+"This is a nice code")# This is an optional block |
| 48 | + |
| 49 | +try: |
| 50 | +print(x/n) |
| 51 | +exceptExceptionase: |
| 52 | +print(Fore.RED+"The second error is: "+str(e)) |
| 53 | +################################################################################################ |