simple python programs examples

Nelson Chege
4 min readJun 23, 2021

having used python for less than two months(from the time writing this post),I have being able to write simple python program that have used some of the libraries in python

I learned python during a two week self-study curriculum that was given by SkaeHub before joining them for their bootcamp. During the two week self study I was introduced to the python syntax, data structure and algorithms.

I was selected for the SkaeHub developer bootcamp this is after the two week self-study curriculum and an on-site interview. During the first two days of the SkaeHub developer bootcamp, I was able to understand some practices used in the professional field which include:

planning of how to deal with the problem

before touching any code first yo have to break down the problem in hand knowing all about the problem and also understanding how the problem can and will be solved and also coming up with the best solution that will accomplish the goal of solving the problem

step by step solution

after breaking down the problem the next step is to write down what step that is going to be taken so as to solve the problem, this is done first without any coding only writing down the solution in pseudo code. After this steps then your can start actual coding

let start the fun part by showing you some of the simple python programs that i was able to create for the first two days of my SkaeHub developer bootcamp

password generator

this python program is able to generate password ,the password generated contains numbers ,symbols ,lowercase and uppercase alphabets. When the program is executed the user can select the strength of the password that they want


import random
import string

# defining the main method

def main():
# select variable is used to get the strength of the password selected_option = int(input("Please enter the password difficulty
level:"" \n 1. Easy \n 2. Medium \n 3.Hard "
" \n 4. Very Hard \n "))

while selected_option < 1 or selected_option > 4:
selected_option = int(input("Please enter the password
difficulty level between 1 and 4:"
" \n 1. Easy \n 2. Medium \n
3.Hard "
" \n 4. Very Hard \n "))
# defining a fuction that generate the password
def get_password(ranged):

# get random password pf length 8 with letters, digits, and
symbols

# string.ascii_letters: To include letters from a-z and A-Z
# string.digits: To include digits from 1 to 10

# string.punctuation: to get special symbols

characters = string.ascii_letters + string.digits + string.punctuation
# range argument is for the length of the password
password = ''.join(random.choice(characters) for i in
range(ranged))

print("your password is: " + password)
# creating an if statement so as to switch between the different strength selected if selected_option == 1: get_password(6) elif selected_option == 2:

get_password(9)

elif selected_option == 3:

get_password(11)

else:

get_password(12)
# calling the main function so as to be executedmain()

duplicate remover

this python program removes duplicate values that are in a list within a python fill

# creating the listlist1 = [15, 9, 5,"a", 3,"a", 5,"b","c", 6, 1]# using str() to convert list to string data typeprint ("The original list is : " + str(list1))# using set() to remove duplicated from listlist2 = list(set(list1))# printing list after removing the duplicatesprint ("The list after removing duplicates : " + str(list2))

check leap year

this python program checks whether if a year that is entered by the user is a leap year or not

# create a function that takes an argument of a yeardef check_leap_year(year):    # set a boolean value of false to leap year    leap = False 

if(year % 4 == 0):

if(year % 100 == 0):

if(year % 400 == 0):

leap = True
# in the if statement if the argument is not true the value of leap will not change to true

return leap
# call the check_leap_year() to execute the functionyear = check_leap_year(int(input("Please enter a year: "))) print(year)

number game

to finish up, this python program is a game in which the python programs generate a random number and ask the user to guess the number just for the fun of it😄

# import randomimport random# random.randint() method from import generate random number and the argumentrandom_number = random.randint(1, 10)#print(random_number)# create empty variable so as to be used in the while loopguess_number = Nonewhile guess_number != random_number:    # user enters the guess      guess_number= int(input("guess a number between 1 and 10: "))

# if statement compairs the user input to the number randomly
selected
if guess_number == random_number:

print("congratulations! you won!")

break
else:

print("nope, sorry. try again!")

this are some of the python programs that i have bein able to create during my SkaeHub developer bootcamp time and having more in my git hub account https://github.com/nelsonchege .i have enjoyed every time during my bootcamp, learning more things that would take me a longer time to learn

--

--