Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Start/Ch2/conditionals_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@
x, y = 10, 100

# conditional flow uses if, elif, else

if x < y :
print("x is less than y")
elif x > y:
print("x is greater than y")
else:
print("x is equal to y")
# conditional statements let you use "a if C else b"
result = "x is less than y" if (x < y) else "x is greater or equal to y"
print(result)
27 changes: 18 additions & 9 deletions Start/Ch2/dict_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@


# Dictionary: a key-value data structure


mydict = {
"one" : 1,
"two" : 2,
3 : "three",
4.5 : ["four" , "point" , "five"]
}
print(mydict)
# dictionaries are accessed via keys


# print(mydict["one"])
# print(mydict[3])
# you can also set dictionary data by creating a new key

# mydict["seven"] = 7
# print(mydict)

# Trying to access a nonexistent key will produce an error


#print(mydict["b"])
# print("two" in mydict)
# print("b" in mydict)
# To avoid this, you can use the "in" operator to see if a key exists


# You can retrieve all of the keys and values from a dictionary

print(mydict.keys())
print(mydict.values())

# You can also iterate over all the items in a dictionary
for key, val in mydict.items():
print(key, val)
43 changes: 32 additions & 11 deletions Start/Ch2/functions_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,38 @@


# define a basic function
print("hello world!")
name = input("What is your name? ")
print("Nice to meet you,", name)

# def hello_func():
# print("hello world!")
# name = input("What is your name? ")
# print("Nice to meet you,", name)
# hello_func()
# function that takes parameters


# def hello_func(greeting):
# print("hello world!")
# name = input("What is your name? ")
# print(greeting, name)
# hello_func("how are you doing")
# hello_func("what's up")
# function that returns a value


# def cube(x):
# return x*x*x
# result = cube(3)
# print(result)
# function with default value for an parameter


# function with variable number of parameters
# def hello_func(greeting, name=None):
# print("hello world!")
# if name == None:
# name = input("What is your name? ")
# print(greeting, name)

# hello_func("how are you doing")
# hello_func(name="nani" , greeting="nice to meet you")

# function with variable nnumber of parameters
def multi_add(start, *args):
result = start
for x in args:
result = result + x
return result

print(multi_add(10,4,5,10,4,10))
3 changes: 3 additions & 0 deletions Start/Ch2/helloworld_start.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# LinkedIn Learning Python course by Joe Marini
# Example file for HelloWorld
print("hello world")
name = input("what is your name? ")
print("nice to meet you ", name)
23 changes: 19 additions & 4 deletions Start/Ch2/loops_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,30 @@
x = 0

# define a while loop

# while x < 5:
# print(x)
# x = x + 1
#answer = input("should i stop?")
# while answer != "yes":
# print(answer)
# answer = input("should I stop?")

# define a for loop


# days = ["mon", "tue", "wed", "thu", "fri","sat","sun"]
# for d in days:
# print(d)
# use a for loop over a collection


# use the break and continue statements

days = ["mon", "tue", "wed", "thu", "fri","sat","sun"]
for d in days:
if (d == "thu"):
#break
continue
print(d)

# using the enumerate() function to get an index and an item
days = ["mon", "tue", "wed", "thu", "fri","sat","sun"]
for i,d in enumerate(days):
print(i,d)
36 changes: 23 additions & 13 deletions Start/Ch2/sequence_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,36 @@

# Sequences: Lists and Tuples
# These are -- surprise -- sequences of values

mylist = [0,1,"two",3.2,False]
print(len(mylist))

# to access a member of a sequence type, use []


# print(mylist[2])
# print(mylist[-1])
# mylist[0] = 10
# print(mylist)
# add a list to another list


# use slices to get parts of a sequence


# another_list = [6,7,8]
# mylist = mylist + another_list
# print(mylist)
# # use slices to get parts of a sequence
# mystr = "this is a string"
# print(mystr[2])
# you can use slices to reverse a sequence

# print("my slice", mylist[1:4])
# print("my slice", mylist[:4:2])
# print("my slice", mylist[::-1])

# Tuples are like lists, but they are immutable


mytuple = (0,1,3,"three")
# print("my tuple", mytuple[::-1])
# Sets are also sequences, but they contain unique values

myset = {1,2,3,3,4,"hey"}
print(myset)
# Set, however, can not be indexed like lists or tuples
# print(myset[0]) # this will cause an error
#print(myset[0]) # this will cause an error

# Test for membership
print(1 in mylist)
print(3 in mytuple)
print(5 in myset)
Empty file added Start/Ch2/test.py
Empty file.
21 changes: 16 additions & 5 deletions Start/Ch2/variables_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@

# We can display the content of a variable using the print() function


print("my integer is ",myint)
print("my string is ",mystr)
# Operators are used to perform operations on variables


# print("my int added to my flot",myint + myfloat)
# print("my int times my flot",myint * myfloat)
# print("my int divided by my flot",myint / myfloat)
# another_str = "this is another string"
# print("string added", mystr + another_str)
# print("nom " * 3)
# Logical and comparison operators


# print(myint == 10)
# print(myfloat != 20)
# print(myint > 20)
# print(myfloat < 10)
# print(myint > 5 and myfloat <25.0)
# print(not(myint > 5 and myfloat <25.0))
# re-declaring a variable works
myint = "abc"
print("change int to string", myint)
19 changes: 12 additions & 7 deletions Start/Ch3/builtin_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,25 @@
mynumbers = [1,3,5,6,9,12,14,17,20,30]

# the len() function calculates the length of a sequence

# print(len(mystring))
# print(len(mynumbers))

# the max() and min() functions will find the largest and smallest value in a sequence


# print(max(mystring))
# print(min(mynumbers))
# the str() function will return a string version of an object
prefix = "result: "
result = 5

# prefix = "result: "
# result = 5
# print(prefix, str(result))

# range(start, stop, step) will create a range of numbers
# You can use ranges along with loops
# for i in range(5,15,3):
# print(i)


# for i in range(5,len(mystring),2):
# print(mystring[i])
# the print function itself is pretty flexible - you can embed variables directly in it
greeting = "Hello!"
count = 10
print(f"{greeting} you are visitor number {count}")
38 changes: 37 additions & 1 deletion Start/Ch3/classes_start.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
# LinkedIn Learning Python course by Joe Marini
# Example file for working with classes
#
class Vehicle:
def __init__(self, bodystyle):
self.bodystyle = bodystyle
def drive(self, speed):
self.mode = "driving"
self.speed = speed

class Car(Vehicle):
def __init__(self, enginetype):
super().__init__("car")
self.wheels = 4
self.doors = 4
self.engine = enginetype
def drive(self, speed):
super().drive(speed)
print(f"driving my {self.engine} car at {self.speed}")

class Motorcycle(Vehicle):
def __init__(self, enginetype, hassidecar):
super().__init__("Motorcycle")
if (hassidecar):
self.wheels = 3
else:
self.wheels = 2
self.doors = 0
self.enginetype = enginetype
def drive(self, speed):
super().drive(speed)
print(f"driving my {self.enginetype} car at {self.speed}")

car1 = Car("gas")
car2 = Car("elecrtric")
mc1 = Motorcycle("gas", True)
print(f"the MC has {mc1.wheels} wheels and engine type is {mc1.enginetype}")
print(f"the car1 has {car1.doors} doors and engine type is {car1.engine}")
car1.drive(20)
car2.drive(40)
mc1.drive(50)
18 changes: 16 additions & 2 deletions Start/Ch3/exceptions_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,24 @@

# Errors can happen in programs, and we need a clean way to handle them
# This code will cause an error because you can't divide by zero:
#x = 10 / 0

# Exceptions provide a way of catching errors and then handling them in
# a separate section of the code to group them together

# try:
# x = 10 / 0
# except:
# print("well that didn't work")

# You can also catch specific exceptions

try:
answer = input("what should i divide 10 by?")
num = int(answer)
print(10/num)
except ZeroDivisionError as e:
print("divistion by zero")
except ValueError as e:
print("you didn't give me a valid number")
print(e)
finally:
print("finally always runs")
14 changes: 8 additions & 6 deletions Start/Ch3/modules_start.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
# Working with modules of code

# import the math module, which contains features for working with mathematics

import math
print("the square root of 16 is ", math.sqrt(16))

# import a specific part of the module so you can refer to it more easily


from math import pi
print("pi",pi)
# import a module and give it a different name


import random as r
print("random", r.randint(110,400))
# the math module contains lots of pre-built functions


Expand All @@ -22,7 +23,7 @@
# try some of the math functions for yourself here:

# Use the 3rd party tabulate module to print tabulated data:

from tabulate import tabulate
# Sample data
data = [
["Product", "Price", "Stock"],
Expand All @@ -32,3 +33,4 @@
]

# Create a formatted table
print(tabulate(data, headers="firstrow",tablefmt="fancy_grid"))