Notes and learnings from Kaggle (Intro to Programming)

 Intro to programming

Arithmetic and Variables

Shift + Enter to run a code
removing # sign is called uncommenting

print("Hello, world")

print(1+2)

print(3**2)

print(((1 + 3) * (9 - 2) / 2) ** 2)

#Multiply 3 by 2
print(3*2)

Multiply 3 by 2

#create test_var and give value
test_var = 4+5
#print the value
print(test_var)

# set the value 3 
my_var = 3
#print the value my_var
print(my_var)
#Set the new value 100
my_var = 100
#print the new my_var
print(my_var)

print(my_var)
print(test_var)

my_var = my_var + 3

#Increase value by 3
my_var = my_var + 3
#print the increased value
print(my_var)

num_years = 4
days_per_year = 365
hours_per_day = 24
seconds_per_hour = 60
secs_per_year = 60
#Calculate umber of seconds of 4 year
total_secs = secs_per_year * seconds_per_hour * hours_per_day * days_per_year * num_years
#print result
print(total_secs)

num_years = 4
days_per_year = 365
hours_per_day = 24
seconds_per_hour = 60
secs_per_year = 60

#update the days per year
days_per_year = 365.25
#Calculate new total_secs
total_secs = secs_per_year * seconds_per_hour * hours_per_day * days_per_year * num_years
print(total_secs)

NameError: print(hours_per_dy)


Functions

function starts with def
argument name will be input_var
the parentheses of function argument(s) need to end by a colon :

#Define the function
def add_three(input_var):
    output_var = input_var + 3
    return output_var

#Run the function with 10 as input
new_number = add_three(10)
print(new_number)

def get_pay(num_hours):
    #pre_taxpay, based on receieving 15/- per hour
    pay_pretax = num_hours * 15
    #after tax pay, based on 12% cut on tax
    pay_aftertax = pay_pretax * (1-.12)
    return(pay_aftertax)
#Calculate pay based on working 40 hours
pay_fulltime = get_pay(40)
print (pay_fulltime)





Data Types


different data types, such as 

integers 
numbers without any fractional part and can be positive/negative/zero

variable x
verify the data type= 'type()'
variable name in parantheses
-
x=14
print (x)
print (type(x))

<class 'int'> refers to the integer data type.

------------------
 
floats
numbers with fractional parts

nearly_pie = 3.141592653
print (nearly_pie)
print (type (nearly_pie))

almost_pie = 22/7
print (almost_pie)
print (type(almost_pie))

<class 'float'> means floats data type.

round() function is useful to round a number.

#round to 5 decimal places
almost_pi = 3.14159
rounded_pi= round(almost_pi, 5)
print (rounded_pi)
print(type(rounded_pi))

y_float = 1.
print (y_float)
print (type(y_float))
Output: 1.0
<class 'float'>


Booleans (True/False)

z_one = True
print (z_one)
print (type(z_one))
Output: True
<class 'bool'>

z_two = False
print (z_two)
print (type(z_two))
Output: False
<class 'bool'>

z_three = 1 < 2
print (z_three)
print (type(z_three))
Output: True
<class 'bool'>

z_four = 5 < 3
print(z_four)
print (type(z_four))
Output: False
<class 'bool'>

z_five = not z_four
print (z_five)
print(type(z_five))
Output: True
<class 'bool'>


Strings
collection of characters (like alphabet letters, punctuation, numerical digits, or symbols)
comes in quotation mark
generally used to present text

w = 'Hello! Python'
print (w)
print(type(w))
print(len(w))
Output: Hello! Python
<class 'str'>
14

shortest_string = ""
print (type(shortest_string))
print(len(shortest_string))
​Output: <class 'str'>
0

shortest_string = ""
print (type(shortest_string))
print(len(shortest_string))
​Output: <class 'str'>
0

#string to float number
y_number ='2.321'
also_my_number = float (my_number)
print (my_number)
print (type(my_number))
Output: 2.321
<class 'str'>

#add two strings
new_string = 'abc' + 'def'
print (new_string)
print (type(new_string))
​Output: abcdef
<class 'str'>

#string*integrar
newest_string = "abc" *3
print (newest_string)
print (type(newest_string))
output: abcabcabc
<class 'str'>


Excercise:
# Define a float
y = 1.
print(y)
print(type(y))
# Convert float to integer with the int function
z = int(y)
print(z)
print(type(z))
Output: 1.0
<class 'float'>
1
<class 'int'>


def get_expected_cost(beds, baths, has_basement):
    value = 80000+ 30000*beds +10000 * baths + 40000* has_basement
    return value


def cost_of_project(engraving, solid_gold):
    cost = solid_gold * (100 + 10 * len(engraving)) + (not solid_gold) * (50 + 7 * len(engraving))
    return cost

def cost_of_project(engraving, solid_gold):
    cost = solid_gold * (100 + 10 * len(engraving)) + (not solid_gold) * (50 + 7 * len(engraving))
    return cost

project_one = cost_of_project("Charlie+Denver", True)
print(project_one)

project_two = cost_of_project("08/10/2000", False)
print(project_two)


Conditions and Conditional Statements


Conditions

In programming, conditions are statements that are either True or False
most common ways of writing conditions just compare two different values

print(2>3)
Output: False

var_one = 1
var_two = 2
​print (var_one < 1)
print (var_two>= var_one)

Output: False
True

 common symbols

Symbol Meaning
== equals
!= does not equal
< less than
<= less than or equal to
> greater than
>= greater than or equal to

Conditional statements
conditions to modify how your function runs

"if" statements

def evaluate_temp(temp):
    #set an initaial message
    message = "Normal Temparature"
    # update value of message only if temparature is greater than 38
    if temp > 38:
        message= "Fever!"
    
    return message


"if ... else" statements
False statement then use "else" 
True statement then use "if"

def evaluate_temp_with_else(temp):
    if temp > 38:
        message = "Fever!"
    else:
        message = "Normal temperature."
    return message

print(evaluate_temp_with_else(37))
 
Output: Normal temperature.


"if ... elif ... else" statements

elif = else if

def evaluate_temp_with_elif(temp):
    if temp > 38:
        message= "Fever!"
    elif temp > 35:
        message= "Normal"
    else:
        message = "low temparature"
    return message
print (evaluate_temp_with_elif(36))
Output: Normal

Examples
def get_taxes(earnings):
    if earnings < 12000:
        tax_owed= .25 * earnings
    if earnings > 12000:
        tax_owed= .30 * earnings
    return tax_owed
ana_taxes = get_taxes (9000)
bob_taxes = get_taxes (15000)
print (ana_taxes)
print(bob_taxes)

Output:
2250.0
4500.0


def add_three_or_eight(number):
    if number < 10:
        result = number + 3
    if number > 10:
        result = number + 8
    return result
print(add_three_or_eight(7))
Output: 10


def add_three_or_eight(number):
    if number < 10:
        result = number + 3
    if number > 10:
        result = number + 8
    return result
print(add_three_or_eight(11))
Output: 19


def get_dose(weight):
    # Dosage is 1.25 ml for anyone under 5.2 kg
    if weight < 5.2:
        dose = 1.25
    elif weight < 7.9:
        dose = 2.5
    elif weight < 10.4:
        dose = 3.75
    elif weight < 15.9:
        dose = 5
    elif weight < 21.2:
        dose = 7.5
    # Dosage is 10 ml for anyone 21.2 kg or over
    else:
        dose = 10
    return dose
print (get_dose(12))

Output: 5
 




Comments

Popular posts from this blog

FAQs for written exam and viva-voce in the field of librarianship: focused on private academic sectors in Bangladesh

Earliest History of Library Education

Notes from Koha LMS - Basics of Admin Training For Librarians conducted by BestBookBuddies