Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 84fbe1b

Browse files
committed
Added finished files
1 parent 4538847 commit 84fbe1b

File tree

14 files changed

+469
-0
lines changed

14 files changed

+469
-0
lines changed

python_sandbox_finished/classes.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# A class is like a blueprint for creating objects. An object has properties and methods(functions) associated with it. Almost everything in Python is an object
2+
3+
# Create class
4+
class User:
5+
# Constructor
6+
def __init__(self, name, email, age):
7+
self.name = name
8+
self.email = email
9+
self.age = age
10+
11+
def greeting(self):
12+
return f'My name is {self.name} and I am {self.age}'
13+
14+
def has_birthday(self):
15+
self.age += 1
16+
17+
18+
# Extend class
19+
class Customer(User):
20+
# Constructor
21+
def __init__(self, name, email, age):
22+
self.name = name
23+
self.email = email
24+
self.age = age
25+
self.balance = 0
26+
27+
def set_balance(self, balance):
28+
self.balance = balance
29+
30+
def greeting(self):
31+
return f'My name is {self.name} and I am {self.age} and my balance is {self.balance}'
32+
33+
# Init user object
34+
brad = User('Brad Traversy', '[email protected]', 37)
35+
# Init customer object
36+
janet = Customer('Janet Johnson', '[email protected]', 25)
37+
38+
janet.set_balance(500)
39+
print(janet.greeting())
40+
41+
brad.has_birthday()
42+
print(brad.greeting())
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# If/ Else conditions are used to decide to do something based on something being true or false
2+
3+
x = 21
4+
y = 20
5+
6+
# Comparison Operators (==, !=, >, <, >=, <=) - Used to compare values
7+
8+
# Simple if
9+
if x > y:
10+
print(f'{x} is greater than {y}')
11+
12+
# If/else
13+
if x > y:
14+
print(f'{x} is greater than {y}')
15+
else:
16+
print(f'{y} is greater than {x}')
17+
18+
# elif
19+
if x > y:
20+
print(f'{x} is greater than {y}')
21+
elif x == y:
22+
print(f'{x} is equal to {y}')
23+
else:
24+
print(f'{y} is greater than {x}')
25+
26+
# Nested if
27+
if x > 2:
28+
if x <= 10:
29+
print(f'{x} is greater than 2 and less than or equal to 10')
30+
31+
32+
# Logical operators (and, or, not) - Used to combine conditional statements
33+
34+
# and
35+
if x > 2 and x <= 10:
36+
print(f'{x} is greater than 2 and less than or equal to 10')
37+
38+
# or
39+
if x > 2 or x <= 10:
40+
print(f'{x} is greater than 2 or less than or equal to 10')
41+
42+
# not
43+
if not(x == y):
44+
print(f'{x} is not equal to {y}')
45+
46+
47+
# Membership Operators (not, not in) - Membership operators are used to test if a sequence is presented in an object
48+
49+
numbers = [1,2,3,4,5]
50+
51+
# in
52+
if x in numbers:
53+
print(x in numbers)
54+
55+
# not in
56+
if x not in numbers:
57+
print(x not in numbers)
58+
59+
# Identity Operators (is, is not) - Compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
60+
61+
# is
62+
if x is y:
63+
print(x is y)
64+
65+
# is not
66+
if x is not y:
67+
print(x is not y)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# A Dictionary is a collection which is unordered, changeable and indexed. No duplicate members.
2+
3+
# Create dict
4+
person = {
5+
'first_name': 'John',
6+
'last_name': 'Doe',
7+
'age': 30
8+
}
9+
10+
# Use constructor
11+
# person2 = dict(first_name='Sara', last_name='Williams')
12+
13+
# Get value
14+
print(person['first_name'])
15+
print(person.get('last_name'))
16+
17+
# Add key/value
18+
person['phone'] = '555-555-5555'
19+
20+
# Get dict keys
21+
print(person.keys())
22+
23+
# Get dict items
24+
print(person.items())
25+
26+
# Copy dict
27+
person2 = person.copy()
28+
person2['city'] = 'Boston'
29+
30+
# Remove item
31+
del(person['age'])
32+
person.pop('phone')
33+
34+
# Clear
35+
person.clear()
36+
37+
# Get length
38+
print(len(person2))
39+
40+
# List of dict
41+
people = [
42+
{'name': 'Martha', 'age': 30},
43+
{'name': 'Kevin', 'age': 25}
44+
]
45+
46+
print(people[1]['name'])

python_sandbox_finished/files.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Python has functions for creating, reading, updating, and deleting files.
2+
3+
# Open a file
4+
myFile = open('myfile.txt', 'w')
5+
6+
# Get some info
7+
print('Name: ', myFile.name)
8+
print('Is Closed : ', myFile.closed)
9+
print('Opening Mode: ', myFile.mode)
10+
11+
# Write to file
12+
myFile.write('I love Python')
13+
myFile.write(' and JavaScript')
14+
myFile.close()
15+
16+
# Append to file
17+
myFile = open('myfile.txt', 'a')
18+
myFile.write(' I also like PHP')
19+
myFile.close()
20+
21+
# Read from file
22+
myFile = open('myfile.txt', 'r+')
23+
text = myFile.read(100)
24+
print(text)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# A function is a block of code which only runs when it is called. In Python, we do not use curly brackets, we use indentation with tabs or spaces
2+
3+
4+
# Create function
5+
def sayHello(name='Sam'):
6+
print(f'Hello {name}')
7+
8+
9+
# Return values
10+
def getSum(num1, num2):
11+
total = num1 + num2
12+
return total
13+
14+
15+
# A lambda function is a small anonymous function.
16+
# A lambda function can take any number of arguments, but can only have one expression. Very similar to JS arrow functions
17+
18+
getSum = lambda num1, num2: num1 + num2
19+
20+
print(getSum(10, 3))

python_sandbox_finished/lists.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# A List is a collection which is ordered and changeable. Allows duplicate members.
2+
3+
# Create 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+
# Get a value
11+
print(fruits[1])
12+
13+
# Get length
14+
print(len(fruits))
15+
16+
# Append to list
17+
fruits.append('Mangos')
18+
19+
# Remove from list
20+
fruits.remove('Grapes')
21+
22+
# Insert into position
23+
fruits.insert(2, 'Strawberries')
24+
25+
# Change value
26+
fruits[0] = 'Blueberries'
27+
28+
# Remove with pop
29+
fruits.pop(2)
30+
31+
# Reverse list
32+
fruits.reverse()
33+
34+
# Sort list
35+
fruits.sort()
36+
37+
# Reverse sort
38+
fruits.sort(reverse=True)
39+
40+
print(fruits)

python_sandbox_finished/loops.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
2+
3+
people = ['John', 'Paul', 'Sara', 'Susan']
4+
5+
# Simple for loop
6+
for person in people:
7+
print(f'Current Person: {person}')
8+
9+
# Break
10+
for person in people:
11+
if person == 'Sara':
12+
break
13+
print(f'Current Person: {person}')
14+
15+
# Continue
16+
for person in people:
17+
if person == 'Sara':
18+
continue
19+
print(f'Current Person: {person}')
20+
21+
# range
22+
for i in range(len(people)):
23+
print(people[i])
24+
25+
for i in range(0, 11):
26+
print(f'Number: {i}')
27+
28+
# While loops execute a set of statements as long as a condition is true.
29+
30+
count = 0
31+
while count < 10:
32+
print(f'Count: {count}')
33+
count += 1

python_sandbox_finished/modules.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# A module is basically a file containing a set of functions to include in your application. There are core python modules, modules you can install using the pip package manager (including Django) as well as custom modules
2+
3+
# Core modules
4+
import datetime
5+
from datetime import date
6+
import time
7+
from time import time
8+
9+
# Pip module
10+
from camelcase import CamelCase
11+
12+
# Import custom module
13+
import validator
14+
from validator import validate_email
15+
16+
# today = datetime.date.today()
17+
today = date.today()
18+
timestamp = time()
19+
20+
c = CamelCase()
21+
# print(c.hump('hello there world'))
22+
23+
email = 'test#test.com'
24+
if validate_email(email):
25+
print('Email is valid')
26+
else:
27+
print('Email is bad')

python_sandbox_finished/myfile.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I love Python and JavaScript I also like PHP

python_sandbox_finished/py_json.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# JSON is commonly used with data APIS. Here how we can parse JSON into a Python dictionary
2+
3+
import json
4+
5+
# Sample JSON
6+
userJSON = '{"first_name": "John", "last_name": "Doe", "age": 30}'
7+
8+
# Parse to dict
9+
user = json.loads(userJSON)
10+
11+
# print(user)
12+
# print(user['first_name'])
13+
14+
car = {'make': 'Ford', 'model': 'Mustang', 'year': 1970}
15+
16+
carJSON = json.dumps(car)
17+
18+
print(carJSON)

0 commit comments

Comments
 (0)