Week 3.2 — Intro to Python
Learning the Basic Grammatical Rules for Python
--
Sorry that this week’s updates have been delayed. Oh, boy. Pfizer hit me quite badly. Anyways, here we are to finally embark on the land of Python. Today, we’ll be exploring the most basic grammatical rules for Python.
1. Variables and Basic Computations
a = 3 # insert 3 into a
b = a # insert a into b
a = a + 1 # insert a+1 into anum1 = a*b # insert a*b into the variable num1
num2 = 99 # insert 99 into the variable num2# variable names can be freely imposed
# yet, it is better to be organized!
2-1. Informational — Strings and Numbers
name = 'bob' # In a variable, a string,
num = 12 # a number, is_number = True # and a "Boolean" can be inserted ########
# so can Lists and Dictionaries as follows:
2-2. Informational — List (same as in Javascript)
a_list = []
a_list.append(1) # insert 1 into the list
a_list.append([2,3]) # insert [2,3] into the list # a_list = [1,[2,3]]
# a_list[0] = 1
# a_list[1] = [2,3]
# a_list[1][0] = 2
2-3. Informational — Dictionary (same as in Javascript)
a_dict = {}
a_dict = {'name':'bob','age':21}
a_dict['height'] = 178# a_dict = {'name':'bob','age':21,'height':178}
# a_dict['name'] = 'bob'
# a_dict['age'] = 21
# a_dict['height'] = 178
2-4. Informational — Dictionary-List Combination
people = [{'name':'bob','age':20},{'name':'carry','age':38}]# people[0]['name'] = 'bob'
# people[1]['name'] = 'carry'person = {'name':'john','age':7
people.append(person)# people = [{'name':'bob','age':20},{'name':'carry','age:38},
{'name':'john','age':7}]
# people[2]['name'] = 'john'
3-1. Functions — Math vs. Javascript vs. Python
# Mathematics:
f(x) = 2*x+3
y = f(2)
y = 7 # Javascript:
function f(x) {
return 2*x+3
} # Python:
def f(x):
return 2*x+3 y = f(2)
display(y) = 7
3-2. Functions — Application
def sum_all(a,b,c):
return a+b+c def mul(a,b):
return a*b result = sum_all(1,2,3) + mul(10,10)
display(result) = 106
4. Conditional (if/else) Statements
def oddeven(num): # define the function
if num%2 == 0 # if the remainder of num when divided by 2 is 0
return True # return "True"
else: # if not
return False # return "False"result = oddeven(20)
display(result) = True
Example:
def is_adult(age):
if age > 20:
print('Adult!')
else:
print('Child!')is_adult(30) = "Adult!"
5. “For” Loops
fruits = [‘apple’, ‘ornage’, ‘banana’, ‘pineapple’]for fruit in fruits:
print(fruit)# apple, orange, banana, and pineapple printed separately
Application:
fruits = [‘apple’, ‘ornage’, 'orange', ‘banana’, ‘pineapple’]count = 0
for fruit in fruits:
if fruit == 'orange':
count += 1print(count)# shows the number of oranges in the list
Advancement:
def count_fruits(target):
count = 0
for fruit in fruits:
if fruit == target:
count += 1
return countapple_count = count_fruits('apple')
print(apple_count) # number of applesorange_count = count_fruits('orange')
print(orange_count) # number of oranges
Another example:
people = [{'name': 'bob', 'age': 20},
{'name': 'carry', 'age': 38},
{'name': 'john', 'age': 7},
{'name': 'smith', 'age': 17},
{'name': 'ben', 'age': 27}]for person in people:
print (person['name'], person['age'])def get_age(myname):
for person in people:
if person['name'] == myname:
return person['age']
else:
return 'n/a'print(get_age('bob')) = 20
print(get_age('brian')) = n/a
Well, that’s about it. I think Python seems much more intuitive than Javascript! Hopefully, I’ll be able to apply these rules for data crawling next time. Yup. That is right. We’ll be covering the application of Python in data crawling in the next post, and it will be enjoyable! See you then.
Fin.