-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirst.py
More file actions
153 lines (109 loc) · 3.23 KB
/
first.py
File metadata and controls
153 lines (109 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/python3
# print strings
print("Hello world!")
print("""This string runs
multiple lines!""")
print("This string is " + "awesome!")
# MATH
print(50 + 50) # add
print(50 - 30) # subtract
print(50 * 50) # multiply
print(50 / 4) # divide
print(50 ** 2) # exponents
print(50 % 6) # modulo - takes what is left over
print(50 / 6) # division with remainder or a float
print(50 // 6) # no remainder
# Variables and methods
quote = "The quieter you become, the more you will hear."
print(quote)
quote = "All is fair in love and war."
print(quote.upper()) # upper
print(quote.lower()) # lower
print(quote.title()) # title case, every first letter of each word
print(len(quote)) # counts characters including spaces
# Methods
age = 31
str1 = "1"
print(age + int(str1))
print("**\n")
# Functions
def testfun():
print("who am I?")
testfun()
def who_am_i(name, age):
print("I'm " + name + " and " + str(age) + " years old.")
who_am_i("M", 31)
def multiply(x, y):
return x * y
print(multiply(3, 4))
print(type(age))
# LISTS - Have brackets []
movies = ["When Harry met Sally", "The Hangover",
"The Perks of Being a Wallflower", "The Exorcist"]
# return the first index number given right until the last number, but not include the last number
print(movies[1:3])
# ["The Hangover", 'The Perks of Being a Wallflower', 'The Exorcist']
print(movies[1:])
print(movies[:1]) # ["When Harry met Sally"]
print(movies[-1]) # The Exorcist # return the last item in the list
print(len(movies)) # count items in the list
movies.append("JAWS")
print(movies)
movies.insert(2, "Hustle")
print(movies)
movies.pop() # remove the last item
print(movies)
movies.pop(0) # remove the first item
print(movies)
amber_movies = ["Just Go With It", "50 First Date"]
our_favorite_movies = movies + amber_movies
print(our_favorite_movies)
grade = [["Bob", 82], ["Alice", 90], ["Jeff", "male"]]
print(grade[2][1])
# Tuples - Do not change, ()
grades = ("a", "b", "c", "d", "f")
print(grades[3])
# For loop - start to finish of an iterate
for x in grades:
print(x)
# While loop - execute as long as True
i = 1
while i < 10:
print(i)
i += 1
# Advanced strings
my_name = "Health"
print(my_name[0])
print(my_name[-1])
sentence = "This is a sentence."
print(sentence[:4])
print(sentence.split()) # delimeter - default is a space
sentence_split = sentence.split()
sentence_join = " ".join(sentence_split)
print(sentence_join)
quote = "He said, 'give me all your money'."
print(quote)
quote = "He said, \"give me all your money\"."
print(quote)
print("A" in "Apple") # True
print("a" in "Apple") # False
print("A".lower() in "Apple".lower()) # improved
movie = "The Hangover"
print("My favorite movie is {}.")
print("My favorite movie is {}.".format(movie))
print("My favorite movie is %s." % movie)
print(f"My favorite movie is {movie}.")
# Dictionaries - key/value pairs {}
# drink is the key, price is the value
drinks = {"White Russian": 7, "Old Fashioned": 10, "Lemon Drop": 8}
employees = {
"Finance": ["Bob", "Linda", "Tina"],
"IT": ["Gene", "Louise"],
"HR": []
}
print(employees)
employees["Legal"]=["Frond", "Jimmy"]
print(employees)
employees.update({"Sales":["Andy", "Olive"]})
print(employees)
print(drinks.get("White Russian"))