Tutorialspoint

Programming Examples

One or more columns in Pandas Dataframe

import pandas as pd

#Sample dataframe
df = pd.DataFrame({
    'DayNo': [1, 2, 3, 4, 5,6,7],
    'Name': ['Sun', 'Mon', 'Tue', 'Wed', 'Thu','Fri','Sat'],
    'Qty': [2.6, 5, 11.8, 2, 5.6,0,0.25]})
# Exisitng Data types
print(df.dtypes)

#Convert to string data type
df_str = df.astype(str)

# Verify the conversion
print("***After Conversion***")
print(df_str.dtypes)

Leap Year in Python

import calendar

print(calendar.isleap(2019))
print(calendar.isleap(2020))

print(calendar.leapdays(2000, 2020))

Number of Days a Month in Python

import calendar

print(calendar.monthrange(2019,2))

Specific Month in Python

import calendar

print (calendar.month(2019,7))

Calendar in python

import calendar

print(calendar.calendar(2019))

HTMLCalendar in Python

import calendar

hc = calendar.HTMLCalendar(calendar.TUESDAY)
a = hc.formatmonth(2019, 3)
print(a)

leapdays in Python

import calendar

print("The number of leap days between 1980 and 2015:")
print (calendar.leapdays(1980, 2015))

First day of the week in Python

import calendar

# print starting day number
print("The number of the first day in calendar is : ")
print(calendar.firstweekday())

Variety of mathematical series in Python

def sumofseries(n, m):
# Convert the digit to string
   str_n = str(n)
   sum_n = n
   sum_all_str = str(n)
   for i in range(1, m):
# Concatenate all strings
      sum_all_str = sum_all_str + str_n

      sum_n = sum_n + int(sum_all_str)
   return sum_n
# Take inputs
n = int(input("Enter the value of n: "))
m = int(input("Enter the value of m: "))
sumofno = sumofseries(n, m)
print("Sum:>",sumofno)

Sieve of Eratosthenes in Python

import time

def sieve_method(n):
#Create a list of prime numbers
    prime_number_list = []
    for i in range(2, n+1):
# Capture the number if it si not in prime list
        if i not in prime_number_list:
            print (i)
# Add the number to the prime list number if it is a multiple 
            for j in range(i*i, n+1, i):
                prime_number_list.append(j)

# Track the Start Time 
StartTime = time.time()
cnt = 0

print(sieve_method(25))

# Track the End Time
EndTime = time.time() 
print 'Time Elapsed is: ', EndTime - StartTime

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.