Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 20fd0e3

Browse files
a
1 parent 60fa4a2 commit 20fd0e3

20 files changed

+771
-0
lines changed
53.7 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
User Interaction
3+
Do the following to solve this problem:
4+
Don't use the input function in this code
5+
1. Store 2 values to num_1 and num_2
6+
2. Use format to output a result such as 1 + 2 = 3
7+
'''
8+
# Assign 2 numbers, num_1 = 1, num_2 = 2
9+
num_1, num_2 = int(1), int(2)
10+
# use format to output 1 + 2 = 3
11+
print("{} + {} = {}".format(num_1, num_2, int(num_1 + num_2)))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'''
2+
Converting from Miles to Kilometers
3+
To solve this problem:
4+
Don't use the input function in this code
5+
1. Assign a value to miles
6+
2. Convert miles to kilometers (1 mile == 1.60934 kilometers and store the result in kilometers
7+
3. Use format to print (ex. 1 miles equals 1.60934 kilometers)
8+
'''
9+
# Assign a value to miles (1 in this case)
10+
miles = int(1)
11+
#Convert to km
12+
kilometers = miles * 1.60935
13+
# Then print this example 1 miles equals 1.60935 kilometers using format
14+
print("{} miles equals {} kilometers".format(miles,kilometers))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Asks the user to input the number of miles
2+
miles = input("Number of miles: ")
3+
# You'll convert miles to kilometers (kilometers = miles * 1.60935)
4+
miles = float(miles)
5+
kilometers = miles * 1.60935
6+
# Then print this example 5 miles equals 8.0467 kilometers
7+
print("{} miles equals {} kilometers".format(miles,kilometers))
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Request user input and print a hello message
2+
name = input('What is your name : ')
3+
print('Hello', name)
4+
5+
# Assign 2 values by splitting user input at the whitespace
6+
num_1, num_2 = input('Enter 2 Numbers : ').split()
7+
# Convert strings into regular numbers (integers)
8+
num_1 = int(num_1)
9+
num_2 = int(num_2)
10+
# Add the values entered and store in sum
11+
sum_1 = num_1 + num_2
12+
13+
# Subtract the values and store in difference
14+
difference = num_1 - num_2
15+
16+
# Multiply the values and store in product
17+
product = num_1 * num_2
18+
19+
# Divide the values and store in quotient
20+
quotient = num_1 / num_2
21+
22+
# Use modulus on the values to find the remainder
23+
remainder = num_1 % num_2
24+
25+
# format() loads the variable values in order into the {} placeholders
26+
print("{} + {} = {}".format(num_1, num_2, sum_1))
27+
print("{} - {} = {}".format(num_1, num_2, difference))
28+
print("{} * {} = {}".format(num_1, num_2, product))
29+
print("{} / {} = {}".format(num_1, num_2, quotient))
30+
print("{} % {} = {}".format(num_1, num_2, remainder))
31+
32+
# Python Problem
33+
'''
34+
I want you to write a program that:
35+
36+
Asks the user to input the number of miles
37+
You’ll convert miles to kilometers (kilometers = miles * 1.60934)
38+
Then print this for example 5 miles equals 8.0467 kilometers
39+
'''
40+
41+
# Solution
42+
# Ask the user to input miles and assign it to the miles variable
43+
miles = input('Enter Miles ')
44+
45+
# Convert from string to integer
46+
miles = int(miles)
47+
48+
# Perform calculation by multiplying 1.60934 times miles
49+
kilometers = miles * 1.60934
50+
51+
# Print results using format()
52+
print("{} miles equals {} kilometers".format(miles, kilometers))
53+
54+
# The Math Module contains many useful functions
55+
# Import the math module
56+
import math
57+
58+
# Because you used import you access methods by referencing the module
59+
print("ceil(4.4) = ", math.ceil(4.4))
60+
print("floor(4.4) = ", math.floor(4.4))
61+
print("fabs(-4.4) = ", math.fabs(-4.4))
62+
63+
# Factorial = 1 * 2 * 3 * 4
64+
print("factorial(4) = ", math.factorial(4))
65+
66+
# Return remainder of division
67+
print("fmod(5,4) = ", math.fmod(5,4))
68+
69+
# Receive a float and return an int
70+
print("trunc(4.2) = ", math.trunc(4.2))
71+
72+
# Return x^y
73+
print("pow(2,2) = ", math.pow(2,2))
74+
75+
# Return the square root
76+
print("sqrt(4) = ", math.sqrt(4))
77+
78+
# Special values
79+
print("math.e = ", math.e)
80+
print("math.pi = ", math.pi)
81+
82+
# Return e^x
83+
print("exp(4) = ", math.factorial(4))
84+
85+
# Return the natural logarithm e * e * e ~= 20 so log(20) tells
86+
# you that e^3 ~= 20
87+
print("log(20) = ", math.log(20))
88+
89+
# You can define the base and 10^3 = 1000
90+
print("log(1000,10) = ", math.log(1000,10))
91+
92+
# You can also use base 10 like this
93+
print("log10(1000) = ", math.log10(1000))
94+
95+
# We have the following trig functions
96+
# sin, cos, tan, asin, acos, atan, atan2, asinh, acosh,
97+
# atanh, sinh, cosh, tanh
98+
# They follow this format
99+
print("sin(0) ", math.sin(0))
100+
101+
# Convert radians to degrees and vice versa
102+
print("degrees(1.5708) = ", math.degrees(1.5708))
103+
print("radians(90) = ", math.radians(90))
32.6 KB
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'''
2+
Determine School Grade
3+
To solve this program:
4+
Don't use th input function in this code
5+
1. Assign an age to the variable age
6+
2. Based on age using conditional and logical operators output one of the following:
7+
a. "Too young for School"
8+
b. "Go to Kindergarten"
9+
c. "Go to Grade [Calculated Grade]"
10+
c. "Go to College"
11+
12+
Going for a minimalist program, did not comment the code
13+
'''
14+
age = int(input("Enter Age: "))
15+
if age < 5: print("Too Young for School")
16+
elif age == 5: print("Go to Kindergarten")
17+
elif (age >= 6) and (age <= 17): print("Go to Grade", age-5)
18+
else: print("Go to College")
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# If age 5 "Go to Kindergarten"
2+
# Ages 6 though 17 goes to grades 1 though 12 "Go to Grade 6"
3+
# If age is greater than 17 then say "Go to College"
4+
age = int(input("Age?: "))
5+
if age == 5:
6+
print("Go to Kindergarten")
7+
elif age >= 6 and age <= 17:
8+
print("Go to Grade", (age-5))
9+
elif age > 17:
10+
print("Go to College")
11+
else:
12+
print("Too young for school")

‎S5_Conditional_Operations/pythontut3.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Conditional Operators
2+
# > : Greater than
3+
# < : Less than
4+
# >= : Greater than or equal to
5+
# <= : Less than or equal to
6+
# == : Equal to
7+
# != : Not equal to
8+
9+
# Drink Question Code
10+
# if, elif, and else are followed by a condition and then a colon
11+
# The code that follows is indented to show what code should be executed depending on the condition
12+
# It is important that the lines be indented exactly the same amount on each line
13+
# I added a default condition with else, so that if the user wanted neither they would get water
14+
drink = input("Pick One (Coke, or Pepsi) : ")
15+
if drink == "Coke":
16+
print("Here is your Coke")
17+
elif drink == "Pepsi":
18+
print("Here is your Pepsi")
19+
else:
20+
print("Here is your water")
21+
22+
# Python Problem for you to Solve
23+
24+
# Taking what you have learned about conditional operators and previous videos, I want you to make a calculator
25+
# You’ll accept 2 numbers separated by an operator
26+
# You’ll then use conditional operators to determine what calculation to make
27+
# Sample output to model :
28+
# Enter Calculation : 5 * 6
29+
# 5 * 6 = 30
30+
31+
# Store the user input of 2 numbers and an operator
32+
num1, operator, num2 = input('Enter Calculation: ').split()
33+
34+
# Convert strings into integers
35+
num1 = int(num1)
36+
num2 = int(num2)
37+
38+
# If, else if (elif) and else execute different code depending on a condition
39+
if operator == "+":
40+
print("{} + {} = {}".format(num1, num2, num1+num2))
41+
42+
# If the 1st condition wasn't true check if this one is
43+
elif operator == "-":
44+
print("{} - {} = {}".format(num1, num2, num1 - num2))
45+
elif operator == "*":
46+
print("{} * {} = {}".format(num1, num2, num1 * num2))
47+
elif operator == "/":
48+
print("{} / {} = {}".format(num1, num2, num1 / num2))
49+
50+
# If none of the above conditions were true then execute this by default
51+
else:
52+
print("Use either + - * or / next time")
53+
54+
# Logical Operators
55+
# and : If both are true it returns true
56+
# or : If either are true it returns true
57+
# not : Converts true into false and vice versa
58+
59+
# Write a program that will determine whether a birthday is important or not
60+
# I’ll use the following criteria to determine that.
61+
62+
# 1 - 18 -> Important
63+
# 21, 50, > 65 -> Important
64+
# All others -> Not Important
65+
66+
# Ask for the users age and cast to an integer
67+
age = int(input("Enter Age : "))
68+
# If age is both greater than or equal to 1 and less than or equal to 18 it is true
69+
if (age >= 1) and (age <= 18):
70+
print("Important Birthday")
71+
# If age is either 21 or 50 then it is true
72+
elif (age == 21) or (age == 50):
73+
print("Important Birthday")
74+
# We check if age is less than 65 and then convert true to false or vice versa
75+
# This is the same as if we put age > 65
76+
elif not(age < 65):
77+
print("Important Birthday")
78+
else:
79+
print("Sorry Not Important")
80+
81+
# 2nd Python Problem for you to Solve
82+
83+
# We’ll determine what grade someone should go to depending on their age. Here is my criteria for determining grade :
84+
# 1. If age 5 "Go to Kindergarten"
85+
# 2. Ages 6 through 17 goes to grades 1 through 12 "Go to Grade 6"
86+
# 3. If age is greater then 17 then say "Go to College"
87+
88+
# Here is sample output :
89+
# Enter age : 6
90+
# Go to Grade 6
91+
92+
# Ask for the age
93+
age = int(input("Enter age: "))
94+
95+
# Handle if age < 5
96+
if age < 5:
97+
print("Too Young for School")
98+
99+
# Special output just for age 5
100+
elif age == 5:
101+
print("Go to Kindergarten")
102+
103+
# Since a number is the result for ages 6 - 17 we can check them all
104+
# with 1 condition
105+
# Use calculation to limit the conditions checked
106+
elif (age > 5) and (age <= 17):
107+
grade = age - 5
108+
print("Go to Grade {}".format(grade))
109+
110+
# Handle everyone else
111+
else:
112+
print("Go to College")
113+
114+
# Ternary Operator
115+
# The ternary operator is used to assign one value or another based on a condition
116+
# It follows this format condition_true if condition else condition_false
117+
118+
age = int(input("What is your age? "))
119+
can_vote = True if age >= 18 else False
120+
print("You can vote :", can_vote)

‎S6_For_and_Range/Python+Tutorial+4.pdf

31.7 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /