Python Basics

ยท

3 min read

PART-1

This Article for Python beginners

You can install Python on your PC or use a cloud environment(codepen/raplit/colab)

Print function

print("Hello World!") 
#output is Hello world!

String Data Type

The string is an array data structure of bytes (or words) that stores a sequence of elements, typically characters, using some character encoding. (Wikipedia)

var_str = "Hashnode"
               ^
               |
          String always write 
          using in the double coma("")
myvar = "John"
myage = "20"
print("My name is " + myvar + "and i am " + myage + "years old")

#output is
My name is john and i am 20 years old

Input Function

the input function is the most important

Syntax 
input("your text")

Python variables

variable rules

3434 = "value"       ---> wrong

23avar = "value"     ---> wrong 

$var = "value"       ---> wrong

var na = "value"     ---> wrong

var = "value"        ---> correct

_var = "value"       ---> correct

var69 = "value"      ---> correct

Primitive Data Types

Have main 4 data types

  1. int

  2. string

  3. bool

  4. float

var1 = 12     ---> int data type
var2 = "John" ---> String data type
var3 = true   ---> boot data type
var4 = 12.5   ---> float data type

Python Operator

source :- https://techvidvan.com/tutorials/python-operators/

source:- techvidvan.com/tutorials/python-operators

Coding exercise

BMI Calculator

height = input("enter your height in m: ")     <--- Input
weight = input("enter your weight in kg: ")    <--- Input
new_w = float(weight)      <--- Type convertion  
new_h = float(height)      <--- Type convertion
bmi = new_w/(new_h*new_h)  <--- calculation logic
print(int(bmi))            <--- output printing 
------------------------
run this your self
#TEST_CASE_01 
#input height = 1.5 , weight = 80 

height = input("enter your height in m: ")     #<--- Input
weight = input("enter your weight in kg: ")    #<--- Input
new_w = float(weight)      #<--- Type convertion  
new_h = float(height)      #<--- Type convertion
bmi = new_w/(new_h*new_h)  #<--- calculation logic
print(int(bmi)) #out put is 35

Tip Calculator

#If the bill was $150.00, split between 5 people, with 12% tip. 

#Each person should pay (150.00 / 5) * 1.12 = 33.6
#Format the result to 2 decimal places = 33.60

#Tip: There are 2 ways to round a number. You might have to do some Googling to solve this.๐Ÿ’ช

#Write your code below this line ๐Ÿ‘‡
#~ example from 100DayOfPython- DR.Angela Yu

bill_v = float(input("Enter the bill value: "))
precentage = int(input("Enter the precentage tip would you like give ? 12 ,13 or 15: "))
people_n = int(input("How many people to split the bill? "))

tip = float(bill_v/people_n * ((precentage/100)+ 1))
tip_n = round(tip,2)
print(f"Tip value is {tip_n}")

BMI 2.0 calculator

height = float(input("enter your height in m: "))
weight = float(input("enter your weight in kg: "))

bmi = (weight / (height * height))

if weight < 18.5:
    mzg = "you are slightly overweight."
else:
    if bmi > 18.5 and bmi < 25:
        mzg = "you have a normal weight."  
    else:
        if bmi > 25 and bmi < 30:
            mzg = "you are slightly overweight."
        else:
            if bmi > 30 and bmi < 35:
                mzg = "you are obese."
            else:
                mzg = "you are clinically obese."                  

bmi_n = round(bmi)

print(f"Your BMI is {bmi_n}, {mzg}")
ย