Thursday, 23 December 2021

Caesar Cipher Python - Program To Encode Text Based On The Concept of Caesar Cipher

Ceaser Cipher is an ancient concept of encoding a message. We will need two parameters here. One is for the text and the next one is for shift(the number of alphabets we are going to shift from the below alphabet list).

For example, if the text is hello and shift is 2, the output will be jgnnq

Caesar Cipher - Python Code

The alphabet variable will contain all the English alphabets. If you take a look below, we have added all the English alphabets two times. It is just done to manage when we are shifting the characters using a large number and avoid ending of the loop.

alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z','a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']


#The variables text and shift are used to get input from the user

text = input("Type your message:\n").lower()

shift = int(input("Type the shift number:\n"))


#We have to create a function called encrypt in which we will encode the passed text

def encrypt(word,shift):

#//newstr is an empty string that will store the encoded character

  newstr="" 

#Below we loop through the characters in the string and encode it based on the shift value.

  for r in word:

    getindex = alphabet.index(r) + int(shift)

    newstr += alphabet[getindex]

  print(f"Actual text is: {text}")

  print(f"Actual text is: {newstr}")


#The below code is the key place where we will call the above function

encrypt(text,shift)


Output:



Leap Year Check Python - Sample Code To Check Leap Year In Python & Return The Number of Days In A Particular Month | Python 100 Days Challenge Udemy Solution

We start with by writing a function called is_leap() that checks if the year entered is a leap year or not. You can check the link to understand more about leap year and how the below code has been built.

#Function to check whether the entered year is a leap year or not which will return
True or False(Boolen) based on the calculation.
def is_leap(year):
  leap_flag = False
  if year % 4 == 0:
    if year % 100 == 0:
      if year % 400 == 0:
        leap_flag = True
      else:
        leap_flag = False
    else:
      leap_flag = True
  else:
    leap_flag = False
  return leap_flag

#days_in_month() - This function has if-else blocs, where first blocks contains days
for non leap year and else block has days for leap year.

def days_in_month(year, month):
  leap_flag_val = is_leap(year)
  #The first if loop checks if the entered year is not a leap year and in the else
   loop it checks for the days in months list for a leap year.
  if leap_flag_val==False:
    month_days = [312831303130313130313031]  
    return month_days[month-1]
  else:
    month_days = [312931303130313130313031]  
    return month_days[month-1]

#You will enter the input values hear
year = int(input("Enter a year: "))
month = int(input("Enter a month: "))
days = days_in_month(year, month)
print(days)

Output:

Wednesday, 22 December 2021

Append Value To A Dictionary/Map in Python | Solution to 100 Days Python Challenge in Udemy

 Please find the below code for appending a value to a Python dictionary. In the below program our aim is to add a new entry to the existing python dictionary.

#First one is the existing travel log list

travel_log = [
{
  "country""France",
  "visits"12,
  "cities": ["Paris""Lille""Dijon"]
},
{
  "country""Germany",
  "visits"5,
  "cities": ["Berlin""Hamburg""Stuttgart"]
},
]

#Defining a function that will add a new entry to the existing travel log.

def add_new_country(country,visits,cities):
    #Below we are creating a dictionary that will take parameters from the function
and append to the travel log.
    
    countrydict = {}
    countrydict["country"] = country
    countrydict["visits"] = visits
    countrydict["cities"] = cities
    travel_log.append(countrydict)

#Below we are calling the function that has been created above

add_new_country("Russia"2, ["Moscow""Saint Petersburg"])
print(travel_log)

Output:

Sunday, 12 December 2021

Python Swap Variable Program - Function For Swapping Variable In Python

 Please find the below code for swapping the variable in Python with line by line explanation


a = input("a: ") // This input commands get the value from user for "a" variable
b = input("b: ") // This input commands get the value from user for "b" variable

var =a; // This line of code is to store value of "a" in different variable in order to
facilitate swap.
a = b; //Now value of "b" is stored in "a"
b=var; //The mdofiied value of "a" is now stored in "b"

print("a: " + a) //This one prints the swapped value of "a"
print("b: " + b) //This one prints the swapped value of "b"

Note: the print function is used to print a value
     the input function is used to get the input value from the user

Sunday, 5 December 2021

Solution For Indentation Error: Unexpected Indent Python

 In python "Indentation Error: Unexpected Indent" is a very important error that you should know.


In python, it is very important to not add unnecessary spaces or a tab in front of a code line. That's the main reason these errors are caused.


For example in the below code, if you see the third line there is additional space before the beginning of the print function.


print("String Manipulation")
print('String Concatenation: done with the "+" sign.')
    print('e.g. print("Hello" + "world")')
print("New lines can be created with a backslash and n.")

The simple thing you need to do in order to overcome this Indentation Error: Unexpected
Indent is to remove the additional space in front of line number 3.


Solution:

print("String Manipulation")
print('String Concatenation: done with the "+" sign.')
print('e.g. print("Hello" + "world")')
print("New lines can be created with a backslash and n.")

This will give you a successful output.