Skip to content

Commit 772d807

Browse files
Caleb CurryCaleb Curry
authored andcommitted
numbers and strings
1 parent 9102cf8 commit 772d807

3 files changed

Lines changed: 251 additions & 3 deletions

File tree

‎beginner_python/01-numbers.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
age = 25
2+
print(age)
3+
4+
#this is a comment
5+
6+
age1 = 1
7+
age_2 = 2
8+
#invalid name -> age-3 = 3
9+
#invalid name -> 4age = 4
10+
Age = 6 #not recommended
11+
age_of_user = 7 #recommend underscores for multiple words based on this guide:
12+
#https://www.python.org/dev/peps/pep-0008/#function-and-variable-names
13+
14+
#return = 5 --> Cannot assign to keywords
15+
#https://docs.python.org/3/reference/lexical_analysis.html#keywords
16+
17+
########## OPERATORS ##########
18+
result = 20 / 3 #6.6666....
19+
print(result)
20+
21+
print("Here is a floor version (crop decimal):", 20 // 3) #6
22+
23+
result = 5**5 #raising a number to a power (5^5)
24+
print(result)
25+
26+
#You can use round instead.
27+
print(round(20/3), 0)
28+
29+
print(result / age) #can use variables in place of literals. This will not change variable values
30+
31+
#guess the output:
32+
print(20 / 3 + 1)
33+
34+
#think of it like this because of operator precedence:
35+
print((20 / 3) + 1)
36+
37+
#You can, however, force certain operations to happen first with ()
38+
print(20 / (3 + 1))
39+
40+
41+
42+
43+
44+
45+
46+
47+

‎beginner_python/02-strings.py‎

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
me = "Caleb"
2+
3+
#escape character example
4+
print("\n")
5+
6+
me = "C\ta\tl\te\tb\n"
7+
8+
print(me)
9+
10+
#can also use single quotes
11+
you = 'Subscriber'
12+
13+
me = "Caleb" #reset to normal
14+
15+
#passing multiple arguments to print
16+
print(me, you)
17+
18+
#double quotes and single quotes work the same way with the exception of working with quotes inside.
19+
#here are some examples:
20+
21+
single_quotes = 'She said "Hi"'
22+
print(single_quotes)
23+
24+
double_quotes = "She said \"Hi\""
25+
print(double_quotes)
26+
27+
single_quotes = 'I\'m learning!'
28+
print(single_quotes)
29+
30+
double_quotes = "I'm learning!"
31+
print(double_quotes)
32+
33+
#notice we have to escape the same quote as the surrounding quote
34+
35+
#Here are the other escape sequences:
36+
#https://docs.python.org/2.0/ref/strings.html
37+
38+
#Notice that if you want to print \ you must put two
39+
print("\\")
40+
41+
#you can also prefix with r which will make a raw string (ignoring escapes except same quote)
42+
print(r'as raw as I\'ve ever seen. \/\/ () \/\/. \t' ) #only \' is escaped
43+
44+
45+
46+
########## CONCATENTATION ##########
47+
48+
#Use a + to concatenate
49+
msg = me + " + " + you
50+
print(msg)
51+
52+
#Can use comma separated values in print. Automatically uses spaces between
53+
print(me, "+", you)
54+
55+
#You can automatically concatenate literals (actual values with quotes as opposed to variables)
56+
#by putting them one after the other. This is ideal if you need to split a large string up
57+
#onto multiple lines.
58+
print("my " "name "
59+
"is " "Caleb")
60+
61+
#You can also use multiline string
62+
print("""Name: Caleb
63+
Age: 58""")
64+
65+
#skip newline using / (without it, it would go down a line)
66+
print("""\
67+
Name: Caleb
68+
Age: 58""")
69+
70+
"""You may see
71+
them as multi-
72+
line comments even
73+
if they technically
74+
are not.
75+
https://stackoverflow.com/questions/7696924/is-there-a-way-to-create-multiline-comments-in-python
76+
"""
77+
78+
########## INDEXES ##########
79+
80+
#It's very common to grab particular characters within a string
81+
#this is also common for collections when we get to em.
82+
msg = "This is a very important message."
83+
print(msg[5])
84+
85+
#indexing is zero based. This means 0 will get the very first character:
86+
print(msg[0])
87+
88+
#This value is returned and can be assigned to a variable or used in an expression
89+
first_letter = msg[0]
90+
print(first_letter + "acos")
91+
92+
#You can also index from the right.
93+
period = msg[32] #from left
94+
print(period)
95+
period = msg[-1] #from right
96+
print(period)
97+
#This may be obvious, but we do not use -0 to get the first element from the right as we would
98+
#use 0 to get the first element from the left. (Side note) -0 actually is 0:
99+
print(-0) #(side note)
100+
print(0 == -0) #0 == 0 (side note)
101+
102+
########## SLICING #########
103+
104+
#repeating this for ease of reading:
105+
msg = "This is a very important message."
106+
107+
#We can pass two numbers in for the index to get a series of characters.
108+
#left is included. Right is excluded.
109+
110+
#this will get 2 characters (from index 1 - 3 with 3 not included...we are left with index 1 and 2.
111+
print(msg[1:3]) #hi
112+
113+
#You can also leave off first to start at beginning
114+
#or leave off second to go to end
115+
print(msg[:5]) #print index 0-4 (because 5 is excluded, remember)
116+
print (msg[1:]) #from index 1 to end
117+
118+
#We can also use negatives. Here is how you get the last 8 characters:
119+
print(msg[-8:]) #start 8 from right and go to end
120+
121+
#out of range index
122+
#Grabbing an out of range index directly will cause an error.
123+
#But incorrect ranges fail gracefully.
124+
#print(msg[42]) #doesn't work
125+
print(msg[42:43]) #works
126+
127+
128+
########## IMMUTABILITY ##########
129+
130+
#Strings are immutable, meaning they can't change.
131+
cant_change = "Java is my favorite!"
132+
133+
#cant_change[0] = K .....nope. Sorry Kava (my dog)
134+
135+
#generate new string from old:
136+
#Kava is my favorite!
137+
new = 'K' + cant_change[1:]
138+
print(new)
139+
140+
#Python is my favorite!
141+
fav_language = "Python " + cant_change[5:]
142+
print(fav_language)
143+
144+
#Java is actually coffee
145+
coffee = cant_change[:8] + "actually coffee" #grab first 7 characters (index 8 not included)
146+
print(coffee)
147+
148+
#operations that appear to change string actually replace:
149+
#Java is actually coffee (contrary to popular belief).
150+
coffee += " (contrary to popular belief)."
151+
print(coffee)
152+
153+
########## GETTING STRING LENGTH ##########
154+
155+
#There is a function we can use....
156+
#similar to how we invoke print to do something for us (output to console)
157+
#we can invoke len to count for us:
158+
159+
print(len(coffee))
160+
161+
#for those from other languages...
162+
#notice we do not say coffee.len()
163+
#coffee.len() XXXXXX
164+
#nope.
165+
166+
#last index is always len() - 1.
167+
name = "Caleb"
168+
print("index 4:", name[4]) #b
169+
print("len(name)", len(name)) #length is 5
170+
171+
172+
########## MORE STRING WORK ##########
173+
174+
#How to convert a number to a string
175+
length = len(name)
176+
177+
print("Length is " + str(length))
178+
179+
#this works however sometimes you just need one combined string instead of components.
180+
#when we use a comma, we are passing in data as separate arguments.
181+
#fortunately, print knows how to handle it. Other times, we must pass in one string.
182+
print("length is ", length)
183+
184+
#an example of this is if we need a variable. We cannot use a comma:
185+
186+
#BAD
187+
msg = "length is", len(name)
188+
print(msg) #NOT WHAT WE WANTED!
189+
190+
#GOOD
191+
length = len(name)
192+
msg = "length is " + str(length))
193+
print(msg)
194+
195+
#EVEN BETTER
196+
#We can also nest method calls:
197+
print("length is " + str(len(name)))
198+
#The order in which these are invoked are in to out...
199+
#len(name) is first which returns a number.
200+
#this number is passed to str which converts it to a string
201+
#this string is then concatenated with the string on the left
202+
#this final string is then passed to print
203+
204+
#That's the end of your introduction to strings!

‎beginner_python/hello.py‎

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)