I'm adding some basic "python practices", questions and solution codes. Practicing while having fun is the best part of learning.
- You can find the Turkish explanation of these practices on my Medium account: https://medium.com/@super.feyza
First Practice:
- Let's calculate in which year you will be 100 years old:
- First of all, to take the instant date, I'll import a libarary which is
-
from datetime import date
- When we run this code, we see the instant date:
-
today = date.today()
-
print("date:", today)
- You will see the date as year-month-day. If you want to write as Day-Month-Year:
-
day_month_year = today.strftime("%d/%m/%Y")
- Since our goal is to find out which year we will be 100 years old, let's take the year from the date (day_month_year):
-
year = int(day_month_year[6:10]) #We made the year which is the string an integer
-
print(f"We are now in {year}") # Output will be "We are in the year now."
- Let's edit the code we write for the user, get some information from the user to reach the result:
-
name = input("What is your name: ")
-
age = int(input("How old are you: "))
- Now that we know the age and date of the user, let's write the final code using a little bit mathematical equation:
-
a = str((year-age)+100)
-
print(name + " will be 100 years old in the year " + a)
Second Practice:
- In this practice, we will write a code to generate passwords with random letters and numbers.
- First of all, let's start by importing the "random" and "string" modules.
-
import random
-
import string
- Then we use the "ASCII" method so that the algorithm can assign random characters from lowercase, uppercase and numbers to us.
-
lower = string.ascii_lowercase #for lowercase
-
upper = string.ascii_uppercase #for uppercase
-
num = string.digits #for numbers
- Now we can combine the characters from lowercase, uppercase, and numbers into a password.
-
all = lower + upper + num
- We can also ask the user how many characters the password they want to create should be.
-
lenght = int(input("Enter the password lenght you want:"))
- To complete the process, we generate a random, user-selected template of the length.
-
template = random.sample(all,lenght)
-
password= "".join(template)
- With the Join() method we take all the elements in an iterable and concatenate them into a single string.
- Last step, let's print the password.
-
print(password)
Third Practice:
- In this practice, we will code a program that calculates the Body Mass Index.
- First of all, we need to get the weight in kilograms and the height in meters from the user.
-
height = float(input("Enter your height in meters: "))
-
weight = float(input("Enter your weight in kilograms: "))
- We abbreviate the body mass index as “MBI” and put the mathematical calculation into code.
-
MBI = weight/(height**2)
- We give the output of the body mass index to the user,
-
print("Your Body Mass Index is: {0:.2f}\n".format(MBI), end='')
- The reason why I use {0:.2f} here: I don't prefer to see 2 numbers after the dot on the screen when MBI is fractional.
- \n is an expression I added because I prefer that the next output starts from a bottom line.
- The user learned the body mass index, but adding whether this value is in the normal range will make the code we wrote more meaningful.
**Fourth Practise:""
- This code will tell you how to solve a problem asked in the "hackerrank": https://www.hackerrank.com/challenges/mini-max-sum/problem?isFullScreen=true
- Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers.
- Then print the respective minimum and maximum values as a single line of two space-separated long integers.
- For example, arr = [1,2,3,4,5] Minimum sum of this list: 1+2+3+4 =10 ; the maximum sum is 2+3+4+5 =14.
- You don't have to write all the codes on this site. That's why you should pay attention to what points they want from you.
- What we need to think about is that if we order our list from smallest to largest, adding 4 values from the beginning and 4 from the end will solve our problem.
-
arr.sort() #arr is the argument of the function they want us to write.
- It doesn't have to be the only solution.
- Now, if we add all the elements of the list and subtract from the last element and the first element, we can find the minimum and maximum values respectively.
-
s = sum(arr)
-
maximum = s - arr[0]
-
minimum = s - arr[len(arr) - 1]