Python Hello World

Sat, 09/23/2017 - 21:07

Decided to start experimenting with Python. I see plenty of jobs available using the language and I am interested so why not.

As usual when learning a new language I start by following the documentation learning how to print, concatenate,...do a little math. Then I see how I might port some of the basic things I might normally do. 

 

import math
# hello world
print("hello world")
# lets do some simple math and append the result to a variable 
four = 2 + 2
# print the result
print(four)
# add a string to a variable
string = "this is a string"
print(string)
# concatnate a string
print(string + " and this is concatnated on to it")
# like javascript a string is also an array
print(string[0])
# print a substring
print(string[0:10])

##
##
##
# lets get the sin and cos of a simple angle
#
# the simple angles
# the first key will be left as 0 because the key is 0 and we need to start with a 1
angles = [0,0,30,45,60,90]

# get sin
def getsin(angles,angle):
    f = 0
    x = -1
    for k,a in enumerate(angles):
        if a == angle:
            f = 1
        if f != 1:
            x+=1
    return math.sqrt(x/2)

# run get sin on the angle ... in this example 60
print(getsin(angles,45))
#
#
# get cosine
def getcos(angles,angle):
    f = 0
    x = -1
    for k,a in enumerate(angles):
        if a == angle:
            f = 1
        if f == 1:
            x+=1
    return math.sqrt(x/2)
#
#
print(getcos(angles,45))

 

Category