Monday 20 November 2023

Password Generator

 

 

 

1
2
3
4
5
import random
passlen = int(input("Enter the length of password"))
s="abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
p = "".join(random.sample(s,passlen ))
print(p)

Output 1:

Enter the length of password
8
I^GOo9)2

Output 2: 


 Enter the length of password
12
pLfdoThK2eny

Value Convertor

 

 

 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def convert_temperature():
    print("\n Which conversion do you want to choose:-")
    print("1. Celsius to Faranheit")
    print("2. Faranheit to Celsius")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        temp = float(input("Enter temperature in celsius: "))
        print(f"{temp} degree celsius is equal to {(temp*9/5)+32} degree faranheit.\n")
    elif choice == 2:
        temp = float(input("Enter temperature in faranheit: "))
        print(f"{temp} degree faranheit is equal to {(temp-32)*5/9} degree celsius.\n")
    else:
        print("Invalid input...please try again\n")

def convert_currency():

    print("\n Which conversion do you want to choose:-")
    print("1. Dollar to pound")
    print("2. Pound to Dollar")
    print("3. Dollar to Rupees")
    print("4. Rupees to Dollar")
    choice = int(input("Enter your choice: "))

    if choice == 1:
        value = float(input("Enter currency in dollars: "))
        print(f"{value} dollars in pounds will be {value*0.73}\n")
    elif choice == 2:
        value = float(input("Enter currency in pounds: "))
        print(f"{value} pounds in dollars will be {value/0.73}\n")
    elif choice == 3:
        value = float(input("Enter currency in dollar: "))
        print(f"{value} dollars in rupees will be {value*83.32}\n")
    elif choice == 4:
        value = float(input("Enter currency in Rupees: "))
        print(f"{value} rupees in dollars will be {value/83.32}\n")

def convert_lengths():

    print("\n Which conversion do you want to choose:-")
    print("1. Centimeters to foot and inches")
    print("2. Foot and inches to centimeter")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        value = float(input("Enter length in cm: "))
        inches = value/2.54
        feet = inches/12
        print(f"{value} centimeters in equal to {feet} feet and {inches} inch\n")
    elif choice == 2:
        feet = float(input("Enter length in feet: "))
        inches = float(input("Enter length in inches: "))
        length = (feet*12 + inches)*2.54
        print(f"{feet} feet and {inches} inches in centimeters will be {length}\n")

print("===== Welcome to Value Converter =====")
while 1:
    print("Which option would you like to choose:-")
    print("1. Convert temperature")
    print("2. Convert currency")
    print("3. Convert lengths")
    print("4. Exit")
    choice = int(input("Enter your choice: "))
    if choice == 1:
        convert_temperature()
    elif choice == 2:
        convert_currency()
    elif choice == 3:
        convert_lengths()
    elif choice == 4:
        print('Exiting...')
        exit(0)

 

 

 

 ===== Welcome to Value Converter =====
Which option would you like to choose:-
1. Convert temperature
2. Convert currency
3. Convert lengths
4. Exit
Enter your choice:
2

 Which conversion do you want to choose:-
1. Dollar to pound
2. Pound to Dollar
3. Dollar to Rupees
4. Rupees to Dollar
Enter your choice:
3
Enter currency in dollar:
20
20.0 dollars in rupees will be 1666.3999999999999

Which option would you like to choose:-
1. Convert temperature
2. Convert currency
3. Convert lengths
4. Exit
Enter your choice:
2

 Which conversion do you want to choose:-
1. Dollar to pound
2. Pound to Dollar
3. Dollar to Rupees
4. Rupees to Dollar
Enter your choice:
4
Enter currency in Rupees:
80
80.0 rupees in dollars will be 0.9601536245799329

Which option would you like to choose:-
1. Convert temperature
2. Convert currency
3. Convert lengths
4. Exit
Enter your choice:
3

 Which conversion do you want to choose:-
1. Centimeters to foot and inches
2. Foot and inches to centimeter
Enter your choice:
1
Enter length in cm:
156
156.0 centimeters in equal to 5.118110236220472 feet and 61.41732283464567 inch

Which option would you like to choose:-
1. Convert temperature
2. Convert currency
3. Convert lengths
4. Exit
Enter your choice:
1

 Which conversion do you want to choose:-
1. Celsius to Faranheit
2. Faranheit to Celsius
Enter your choice:
1
Enter temperature in celsius:
32
32.0 degree celsius is equal to 89.6 degree faranheit.

Which option would you like to choose:-
1. Convert temperature
2. Convert currency
3. Convert lengths
4. Exit
Enter your choice:
4
Exiting...

User Management System

 

 

 

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
database = {'entries': []}

SRNO = 'srno'
NAME = 'name'
AGE = 'age'
GENDER = 'gender'
OCCUPATION = 'occupation'

def get_serial_no():
    return len(database['entries']) + 1

def add_entry(entry):
    entry = {
        'srno': get_serial_no(),
        'name': entry['name'],
        'age': entry['age'],
        'gender': entry['gender'],
        'occupation': entry['occupation']
    }
    database['entries'].append(entry)

def check_entry_presence(value):
    for num, entry in enumerate(database['entries']):
        if entry[value[0]] == value[1]:
            return 1
    return 0

def search_entry(value):
    for num, entry in enumerate(database['entries']):
        if entry[value[0]] == value[1]:
            return entry

def update_entry(value, updated_entry):
    for num, entry in enumerate(database['entries']):
        if entry[value[0]] == value[1]:
            database['entries'][num] == updated_entry

def delete_entry(value):
    for num, entry in enumerate(database['entries']):
        if entry[value[0]] == value[1]:
            database['entries'].remove(entry)

def display_entry(entry):
    print(f"SRNO: {entry['srno']}")
    print(f"Name: {entry['name']}")
    print(f"Age: {entry['age']}")
    print(f"Gender: {entry['gender']}")
    print(f"Occupation: {entry['occupation']}\n")

def display_all_entries():
    for entry in database["entries"]:
        display_entry(entry)

def select_entry_and_value():
    value_type = ''
    value = ''
    while 1:
        print('Choose an entry based on which to search entries in database: ')
        print("1. srno")
        print("2. name")
        print("3. age")
        print("4. gender")
        print("5. occupation")
        choice = int(input("Enter your choice: "))
        if choice < 1 or choice > 5:
            print("Invalid input...please try again")
        else:
            if choice == 1:
                value_type = SRNO
                value = input("Enter serial number to search: ")
                return (value_type, value)
            elif choice == 2:
                value_type = NAME
                value = input("Enter name to search: ")
                return (value_type, value)
            elif choice == 3:
                value_type = AGE
                value = input("Enter age to search: ")
                return (value_type, value)
            elif choice == 4:
                value_type = GENDER
                value = input("Enter gender to search: ")
                return (value_type, value)
            elif choice == 5:
                value_type = OCCUPATION
                value = input("Enter occupation to search: ")
                return (value_type, value)

def get_entry_details():
    output = {}
    output[NAME] = input("Enter name: ")
    output[AGE] = input("Enter age: ")
    output[GENDER] = input("Enter gender: ")
    output[OCCUPATION] = input("Enter occupation: ")
    return output

print("===== Welcome To User Management System =====")
while 1:
    print("\n What would you like to do:-")
    print("1. Add an entry")
    print("2. Update an entry")
    print("3. Delete an entry")
    print("4. Search an entry")
    print("5. Display all entries")
    print("6. Exit")
    choice = int(input("Enter your choice: "))
    if choice > 7 or choice < 1:
        print("Invalid input...please try again")
    else:
        if choice == 1:
            print("Enter details for the new entry:-")
            entry = get_entry_details()
            add_entry(entry)
            print("Entry successfully created...")
        elif choice == 2:
            value = select_entry_and_value()
            print('Enter the details of the updated entry:-')
            entry = get_entry_details()
            update_entry(value, entry)
            print("Entry successfully updated...")
        elif choice == 3:
            value = select_entry_and_value()
            delete_entry(value)
            print("Entry successfully deleted...")
        elif choice == 4:
            value = select_entry_and_value()
            entry = search_entry(value)
            display_entry(entry)
        elif choice == 5:
            display_all_entries()
        elif choice == 6:
            print('Exiting')
            break

Rock Paper Scissors Game Using Python

  



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import random
player1=input('Select Rock, Paper, or Scissors :').lower()
print("Player 1 selected:",player1)
player2=random.choice(['Rock','Paper','Scissors']).lower()
print("Player 2 selected:",player2)

if player1=='rock' and player2=='paper':
	print("Player 2 Won !")
elif player1=='paper' and player2=='scissors':
	print("Player 2 Won!")
elif player1=='scissors' and player2=='rock':
	print("Player 2 Won !")
elif player1==player2:
	print("It's A Tie !")
else:
	print("Player 1 Won !")

Output 1:

Select Rock, Paper, or Scissors :
rock
Player 1 selected: rock
Player 2 selected: paper
Player 2 Won !

Output 2:

Select Rock, Paper, or Scissors :
Paper
Player 1 selected: paper
Player 2 selected: scissors
Player 2 Won!
 

Output 3:

Select Rock, Paper, or Scissors :
Scissors
Player 1 selected: scissors
Player 2 selected: rock
Player 2 Won !

Output 4:

Select Rock, Paper, or Scissors :
paper
Player 1 selected: paper
Player 2 selected: paper
It's A Tie !




Story Generator in Python

 Using Random Module You Can Generate a random story 


1
2
3
4
5
6
7
8
import random
when=['A few years ago','Yesterday','Last night','A long time ago','On 20th Jan']
who=['a rabbit','an elephant','a mouse','a turtle','a cat']
namee=['Ali','Mariam','Daniel','Houck','Starwalker']
residence=['Barcelona','India','Germany','London','England']
went=['cinema','university','seminar','school','laundary']
happened=['made a lot of friends','eats a burger','found a secret key','solved a mystery','wrote a book']
print(random.choice(when)+', '+random.choice(namee)+' that lived in '+ random.choice(residence)+', went to the '+random.choice(went)+' and '+random.choice(happened))



Output 1:

A long time ago, Mariam that lived in Germany, went to the cinema and eats a burger

Output 2: 

Last night, Ali that lived in Germany, went to the seminar and solved a mystery

Saturday 11 November 2023

Pandas

This tutorial will explore the most potent Python library pandas,  and we will discuss the most important functions of this library that are important for data analysis. Beginners can also follow this tutorial due to its simplicity and efficiency. If you don’t have python installed in your system, you can use Google Colaboratory.

 

Importing Data

 You can download the dataset from that link.

 

import pandas as pd

df = pd.read_csv("kaggle_sales_data.csv", encoding="Latin-1")  # Load the data

 

df.head()  # Show first five rows

 Output:

 10 Essential Pandas Functions Every Data Scientist Should Know

 

Data Exploration

 In this section, we will discuss various functions that help you to get more about your data. Like viewing it or getting the mean, average, min/max, or getting information about the dataframe.

 

1. Data Viewing

 df.head(): It displays the first five rows of the sample data

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

2.     df.tail(): It displays the last five rows of the sample data

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

3.     df.sample(n): It displays the random n number of rows in the sample data

df.sample(6)

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

4.     df.shape: It displays the sample data's rows and columns (dimensions).

(2823, 25)

 It signifies that our dataset has 2823 rows, each containing 25 columns.

 

2. Statistics

 This section contains the functions that help you perform statistics like average, min/max, and quartiles on your data.

1.     df.describe(): Get the basic statistics of each column of the sample data

 

10 Essential Pandas Functions Every Data Scientist Should Know


 
df.info(): Get the information about the various data types used and the non-null count of each column.

 

 10 Essential Pandas Functions Every Data Scientist Should Know


 

2.     df.corr(): This can give you the correlation matrix between all the integer columns in the data frame.

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

4.     df.memory_usage(): It will tell you how much memory is being consumed by each column.

 10 Essential Pandas Functions Every Data Scientist Should Know

 

3.    Data Selection

 You can also select the data of any specific row, column, or even multiple columns.

1.     df.iloc[row_num]:  It will select a particular row based on its index

For ex-,

df.iloc[0]

 df[col_name]: It will select the particular column

For ex-,

df["SALES"]

 Output:

 10 Essential Pandas Functions Every Data Scientist Should Know

 

3.     df[[‘col1’, ‘col2’]]: It will select multiple columns given

For ex-,

df[["SALES", "PRICEEACH"]]

 Output:

 10 Essential Pandas Functions Every Data Scientist Should Know

 

 

4. Data Cleaning

 These functions are used to handle the missing data. Some rows in the data contain some null and garbage values, which can hamper the performance of our trained model. So, it is always better to correct or remove these missing values.

1.     df.isnull(): This will identify the missing values in your dataframe.

2.     df.dropna(): This will remove the rows containing missing values in any column.

3.     df.fillna(val): This will fill the missing values with val given in the argument.

4.     df[‘col’].astype(new_data_type): It can convert the data type of the selected columns to a different data type.

For ex-,

df["SALES"].astype(int)

 

We are converting the data type of the SALES column from float to int.

 10 Essential Pandas Functions Every Data Scientist Should Know

 

 5. Data Analysis

 Here, we will use some helpful functions in data analysis, like grouping, sorting, and filtering.

1.     Aggregation Functions:

You can group a column by its name and then apply some aggregation functions like sum, min/max, mean, etc.

df.groupby("col_name_1").agg({"col_name_2": "sum"})

 For ex-,

df.groupby("CITY").agg({"SALES": "sum"})

 

It will give you the total sales of each city.

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

If you want to apply multiple aggregations at a single time, you can write them like that.

For ex-,

aggregation = df.agg({"SALES": "sum", "QUANTITYORDERED": "mean"})

 

Output:

SALES              1.003263e+07

 

QUANTITYORDERED    3.509281e+01

 

dtype: float64

 

2.     Filtering Data:

We can filter the data in rows based on a specific value or a condition.

For ex-,

df[df["SALES"] > 5000]

 

Displays the rows where the value of sales is greater than 5000

You can also filter the dataframe using the query() function. It will also generate a similar output as above.

For ex,

df.query("SALES" > 5000)

 

3.     Sorting Data:

You can sort the data based on a specific column, either in the ascending order or in the descending order.

For ex-,

df.sort_values("SALES", ascending=False)  # Sorts the data in descending order

 

4.     Pivot Tables:

We can create pivot tables that summarize the data using specific columns. This is very useful in analyzing the data when you only want to consider the effect of particular columns.

For ex-,

pd.pivot_table(df, values="SALES", index="CITY", columns="YEAR_ID", aggfunc="sum")

 

Let me break this for you.

1.     values: It contains the column for which you want to populate the table's cells.

2.     index: The column used in it will become the row index of the pivot table, and each unique category of this column will become a row in the pivot table.

3.     columns: It contains the headers of the pivot table, and each unique element will become the column in the pivot table. 

4.     aggfunc: This is the same aggregator function we discussed earlier.

Output:

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

This output shows a chart which depicts the total sales in a particular city for a specific year.

 

6. Combining Data Frames

 We can combine and merge several data frames either horizontally or vertically. It will concatenate two data frames and return a single merged data frame.

For ex-,

combined_df = pd.concat([df1, df2])

 

You can merge two data frames based on a common column. It is useful when you want to combine two data frames that share a common identifier.

For ex,

merged_df = pd.merge(df1, df2, on="common_col")

 

7. Applying Custom Functions

 

You can apply custom functions according to your needs in either a row or a column.

For ex-,

def cus_fun(x):

    return x * 3

 

df["Sales_Tripled"] = df["SALES"].apply(cus_fun, axis=0)

 

We have written a custom function that will triple the sales value for each row. axis=0 means that we want to apply the custom function on a column, and axis=1 implies that we want to apply the function on a row.

In the earlier method you have to write a separate function and then to call it from the apply() method. Lambda function helps you to use the custom function inside the apply() method itself. Let’s see how we can do that.

df["Sales_Tripled"] = df["SALES"].apply(lambda x: x * 3)

 

Applymap:

We can also apply a custom function to every element of the dataframe in a single line of code. But a point to remember is that it is applicable to all the elements in the dataframe.

For ex-,

df = df.applymap(lambda x: str(x))

 It will convert the data type to a string of all the elements in the dataframe.

 8. Time Series Analysis

 In mathematics, time series analysis means analyzing the data collected over a specific time interval, and pandas have functions to perform this type of analysis.

Conversion to DateTime Object Model:

We can convert the date column into a datetime format for easier data manipulation.

For ex-,

df["ORDERDATE"] = pd.to_datetime(df["ORDERDATE"])

 

Output:

 

10 Essential Pandas Functions Every Data Scientist Should Know


 

Calculate Rolling Average:

Using this method, we can create a rolling window to view data. We can specify a rolling window of any size. If the window size is 5, then it means a 5-day data window at that time. It can help you remove fluctuations in your data and help identify patterns over time.

For ex-

rolling_avg = df["SALES"].rolling(window=5).mean()

 Output:

 

10 Essential Pandas Functions Every Data Scientist Should Know

 

9. Cross Tabulation

 We can perform cross-tabulation between two columns of a table. It is generally a frequency table that shows the frequency of occurrences of various categories. It can help you to understand the distribution of categories across different regions.

For ex-,

Getting a cross-tabulation between the COUNTRY and DEALSIZE.

cross_tab = pd.crosstab(df["COUNTRY"], df["DEALSIZE"])

 

It can show you the order size (‘DEALSIZE’) ordered by different countries.

 

10 Essential Pandas Functions Every Data Scientist Should Know

 

10. Handling Outliers

 Outliers in data means that a particular point goes far beyond the average range. Let’s understand it through an example. Suppose you have 5 points, say 3, 5, 6, 46, 8. Then we can clearly say that the number 46 is an outlier because it is far beyond the average of the rest of the points. These outliers can lead to wrong statistics and should be removed from the dataset.

Here pandas come to the rescue to find these potential outliers. We can use a method called Interquartile Range(IQR), which is a common method for finding and handling these outliers. You can also read about this method if you want information on it. You can read more about them here.

Let’s see how we can do that using pandas.

Q1 = df["SALES"].quantile(0.25)

Q3 = df["SALES"].quantile(0.75)

IQR = Q3 - Q1

lower_bound = Q1 - 1.5 * IQR

upper_bound = Q3 + 1.5 * IQR

 

outliers = df[(df["SALES"] < lower_bound) | (df["SALES"] > upper_bound)]

 

Q1 is the first quartile representing the 25th percentile of the data and Q3 is the third quartile representing the 75th percentile of the data.

lower_bound variable stores the lower bound that is used for finding potential outliers. Its value is set to 1.5 times the IQR below Q1. Similarly, upper_bound calculates the upper bound, 1.5 times the IQR above Q3.

After which, you filter out the outliers that are less than the lower or greater than the upper bound.

 

Wrapping it Up

 Python pandas library enables us to perform advanced data analysis and manipulations. These are only a few of them. You can find some more tools in this pandas documentation. One important thing to remember is that the selection of techniques can be specific which caters to your needs and the dataset you are using.Top of Form