8000 Initial commit · apt-grid/learning-python-2896241@019cea8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 019cea8

Browse files
committed
Initial commit
1 parent 1b83d7d commit 019cea8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1295
-0
lines changed

Ch2 - Basics/challenge_solution.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Solution to programming challenge for Learning Python course
2+
# LinkedIn Learning Python course by Joe Marini
3+
#
4+
5+
def is_palindrome(teststr):
6+
# use the slice trick to reverse the string
7+
if teststr == teststr[::-1]:
8+
return True
9+
return False
10+
11+
run = True
12+
while (run):
13+
teststr = input("Enter string to test for palindrome or 'exit':")
14+
15+
# If the user types "exit" then quit the program
16+
if teststr == "exit":
17+
run = False
18+
break
19+
20+
# convert the string to all lower case
21+
teststr = teststr.lower()
22+
23+
# strip all the spaces and punctuation from the string
24+
newstr = ""
25+
for x in teststr:
26+
if x.isalnum():
27+
newstr += x
28+
29+
print("Palindrome test:", is_palindrome(newstr))

Ch2 - Basics/classes_finished.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Example file for working with classes
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
class Vehicle():
7+
def __init__(self, bodystyle):
8+
self.bodystyle = bodystyle
9+
10+
def drive(self, speed):
11+
self.mode = "driving"
12+
self.speed = speed
13+
14+
15+
class Car(Vehicle):
16+
def __init__(self, enginetype):
17+
super().__init__("Car")
18+
self.wheels = 4
19+
self.doors = 4
20+
self.engine = enginetype
21+
22+
def drive(self, speed):
23+
super().drive(speed)
24+
print("Driving my", self.engine, "Car at ", self.speed)
25+
26+
27+
class Motorcycle(Vehicle):
28+
def __init__(self, enginetype, hassidecar):
29+
super().__init__("Motorcycle")
30+
if (hassidecar):
31+
self.wheels = 2
32+
else:
33+
self.wheels = 3
34+
self.doors = 0
35+
self.engine = enginetype
36+
37+
def drive(self, speed):
38+
super().drive(speed)
39+
print("Driving my", self.engine, "motorcylce at ", self.speed)
40+
41+
42+
car1 = Car("gas")
43+
car2 = Car("electric")
44+
mc1 = Motorcycle("gas", True)
45+
46+
print(mc1.wheels)
47+
print(car1.engine)
48+
print(car2.doors)
49+
50+
car1.drive(30)
51+
car2.drive(40)
52+
mc1.drive(50)

Ch2 - Basics/classes_start.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#
2+
# Example file for working with classes
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+

Ch2 - Basics/conditionals_finished.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# Example file for working with conditional statements
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
7+
8+
def main():
9+
x, y = 10, 100
10+
11+
# conditional flow uses if, elif, else
12+
if x < y:
13+
result = "x is less than y"
14+
elif x == y:
15+
result = "x is same as y"
16+
else:
17+
result = "x is greater than y"
18+
print(result)
19+
20+
# conditional statements let you use "a if C else b"
21+
result = "x is less than y" if (x < y) else "x is greater than or equal to y"
22+
print(result)
23+
24+
# Python does not have support for higher-order conditionals
25+
# like "switch-case" in other languages
26+
27+
28+
if __name__ == "__main__":
29+
main()

Ch2 - Basics/conditionals_start.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#
2+
# Example file for working with conditional statements
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
7+
8+
def main():
9+
x, y = 10, 100
10+
11+
# conditional flow uses if, elif, else
12+
13+
# conditional statements let you use "a if C else b"
14+
15+
16+
if __name__ == "__main__":
17+
main()

Ch2 - Basics/exceptions_finished.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#
2+
# Example file for working with classes
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
# Errors can happen in programs, and we need a clean way to handle them
7+
# This code will cause an error because you can't divide by zero:
8+
# x = 10 / 0
9+
10+
# Exceptions provide a way of catching errors and then handling them in
11+
# a separate section of the code to group them together
12+
try:
13+
x = 10 / 0
14+
except:
15+
print("Well that didn't work!")
16+
17+
# You can also catch specific exceptions
18+
try:
19+
answer = input("What should I divide 10 by?")
20+
num = int(answer)
21+
print(10 / num)
22+
except ZeroDivisionError as e:
23+
print("You can't divide by zero!")
24+
except ValueError as e:
25+
print("You didn't give me a valid number!")
26+
print(e)
27+
finally:
28+
print("The finally section always runs")
29+

Ch2 - Basics/exceptions_start.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#
2+
# Example file for working with classes
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
# Errors can happen in programs, and we need a clean way to handle them
7+
# TODO: This code will cause an error because you can't divide by zero:
8+
9+
# TODO: Exceptions provide a way of catching errors and then handling them in
10+
# a separate section of the code to group them together
11+
12+
13+
# TODO: You can also catch specific exceptions
14+

Ch2 - Basics/functions_finished.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# Example file for working with functions
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
7+
# define a basic function
8+
def func1():
9+
print("I am a function")
10+
11+
# function that takes arguments
12+
def func2(arg1, arg2):
13+
print(arg1, " ", arg2)
14+
15+
# function that returns a value
16+
def cube(x):
17+
return x*x*x
18+
19+
# function with default value for an argument
20+
def power(num, x=1):
21+
result = 1
22+
for i in range(x):
23+
result = result * num
24+
return result
25+
26+
# function with variable number of arguments
27+
def multi_add(*args):
28+
result = 0
29+
for x in args:
30+
result = result + x
31+
return result
32+
33+
34+
func1()
35+
print(func1())
36+
print(func1)
37+
func2(10, 20)
38+
print(func2(10, 20))
39+
print(cube(3))
40+
print(power(2))
41+
print(power(2, 3))
42+
print(power(x=3, num=2))
43+
print(multi_add(4, 5, 10, 4))

Ch2 - Basics/functions_start.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Example file for working with functions
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
7+
# TODO: define a basic function
8+
9+
10+
# TODO: function that takes arguments
11+
12+
13+
# TODO: function that returns a value
14+
15+
16+
# TODO: function with default value for an argument
17+
18+
19+
# TODO: function with variable number of arguments
20+
21+

Ch2 - Basics/helloworld_finished.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Example file for HelloWorld
3+
# LinkedIn Learning Python course by Joe Marini
4+
#
5+
6+
7+
def main():
8+
print("hello world!")
9+
name = input("What is your name? ")
10+
print("Nice to meet you,", name)
11+
12+
if __name__ == "__main__":
13+
main()

0 commit comments

Comments
 (0)
0