Skip to content

Commit 8674673

Browse files
committed
tutorial complete through functions
1 parent 84fbe1b commit 8674673

5 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
11
# A function is a block of code which only runs when it is called. In Python, we do not use parentheses and curly brackets, we use indentation with tabs or spaces
22

3+
#Create a function
4+
#pass in a default value by setting name = to "World"
5+
6+
def sayHello(name = "World"):
7+
print(f"Hello, {name}")
8+
9+
sayHello('Dustin Huth')
10+
11+
#Return Values
12+
13+
def getSum(num1, num2):
14+
total = num1 + num2
15+
return total
16+
17+
print('getSum(4, 3): ', getSum(4, 3))
18+
319

420

521
# A lambda function is a small anonymous function.
622
# A lambda function can take any number of arguments, but can only have one expression. Very similar to JS arrow functions
723

24+
getSum = lambda num1, num2 : num1 + num2
25+
print('lambda getSum(10, 3): ', getSum(10, 3))

‎python_sandbox_starter/lists.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
11
# A List is a collection which is ordered and changeable. Allows duplicate members.
2+
3+
# Create a list
4+
numbers = [1, 2, 3, 4, 5]
5+
fruits = ['apples', 'oranges', 'grapes', 'pears']
6+
7+
# use a constructor
8+
numbers2 = list((1, 2, 3, 4, 5))
9+
10+
print(numbers, numbers2)
11+
12+
# get a single value
13+
print(fruits[1])
14+
15+
#length of array
16+
print(len(fruits))
17+
18+
#append to the end
19+
fruits.append('Mangoes'.lower())
20+
print('append', fruits)
21+
22+
#remove maching item
23+
fruits.remove('grapes')
24+
print('remove', fruits)
25+
26+
#insert into a specific index
27+
fruits.insert(2, 'strawberries')
28+
print('insert', fruits)
29+
30+
#remove by position (pop method)
31+
fruits.pop(2)
32+
print('pop', fruits)
33+
34+
#reverse list
35+
fruits.reverse()
36+
print('reverse', fruits)
37+
38+
#sort (alphabetical)
39+
fruits.sort()
40+
print('sort', fruits)
41+
42+
43+
#reverse Sort
44+
fruits.sort(reverse = True)
45+
print('reverse sort', fruits)
46+
47+
#change value
48+
fruits[0] = 'monkeys'
49+
print('change value', fruits)
50+

‎python_sandbox_starter/strings.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# Strings in python are surrounded by either single or double quotation marks. Let's look at string formatting and some string methods
22

3+
name = 'Dustin'
4+
age = 40
35

46
# String Formatting
57

8+
# Concatenating
9+
print('Hello. My name is, ' + name + '. I am ' + str(age) + ' years old!')
10+
11+
#Arguments by position
12+
print('My name is, {name}. I am {age} years old.'.format(name= name, age=age))
13+
14+
#F-Strings (Python 3.6^)
15+
print(f"My name is, {name}. I'm {age} years of age")
16+
617
# String Methods
18+
19+
s = 'hello world'.upper()
20+
print(s.capitalize())
21+
print(s)
22+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
# A Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
2+
#create a tuple literal
3+
fruits = ('apples', 'oranges', 'grapes')
4+
5+
#create a tuple using a contsructor
6+
fruits2 = tuple(('apples', 'oranges', 'grapes'))
27

8+
print('fruits: ', fruits, 'fruits2: ', fruits2)
9+
10+
#tuple with only 1 value needs a trailing comma to be interpreted as a tuple
11+
12+
fruits3 = ('apples',)
13+
print('trailing comma for single item: ', fruits3, 'type', type(fruits3))
14+
15+
#get value
16+
print('get value by index [0]', fruits[0])
17+
18+
#replace value at index doesn't work because tuples are immutable
19+
#fruits[0] = 'Cherries'
20+
#print('immutable (expect error:)', fruits)
21+
22+
#delete
23+
24+
del fruits2
25+
#print('delete: ', fruits2)
326

27+
#length
28+
print('len', len(fruits))
429

530
# A Set is a collection which is unordered and unindexed. No duplicate members.

‎python_sandbox_starter/variables.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@
1313
- Must start with a letter or an underscore
1414
- Can have numbers but can not start with one
1515
"""
16+
x = 1 #int
17+
y = 2.5 #float
18+
name = 'John' #str
19+
is_cool = True #bool
20+
21+
# multiple assignments
22+
23+
a, b, first_name, is_smart = (1, 2.5, 'Dustin', True)
24+
25+
print('Hello')
26+
print(x, y, a*b, first_name, is_smart)
27+
print(type(x))
28+
29+
# casting
30+
x = str(x)
31+
print(type(x))

0 commit comments

Comments
 (0)