-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctions-PRINT.py
More file actions
291 lines (196 loc) · 7.68 KB
/
Functions-PRINT.py
File metadata and controls
291 lines (196 loc) · 7.68 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
########## PRINT() ##########
print()
#1. The print() function is a built-in function. It prints/outputs a specified message to the screen/consol window
#2. Built-in functions, contrary to user-defined functions, are always available and don't have to be imported
# Python 3.7.1 comes with 69 built-in functions
# You can find their full list provided in alphabetical order in the Python Standard Library
#3. To call a function (function invocation), you need to use the function name followed by parentheses
# You can pass arguments into a function by placing them inside the parentheses. You must separate arguments
# with a comma, e.g., print("Hello,", "world!"). An "empty" print() function outputs an empty line to the screen
#4. Python strings are delimited with quotes, e.g., "I am a string", or 'I am a string, too'
#5. Computer programs are collections of instructions. An instruction is a command to perform
# a specific task when executed, e.g., to print a certain message to the screen
#6. In Python strings the backslash (\) is a special character which announces that the next
# character has a different meaning, e.g., \n (the newline character) starts a new output line
#7. Positional arguments are the ones whose meaning is dictated by their position
# the second argument is outputted after the first, the third is outputted after the second, etc
#8. Keyword arguments are the ones whose meaning is not dictated by their location
# but by a special word (keyword) used to identify them
#9. The end and sep parameters can be used for formatting the output of the print() function
# The sep parameter specifies the separator between the outputted arguments (e.g., print("H", "E", "L", "L", "O", sep="-")
# whereas the end parameter specifies what to print at the end of the print statement
## print("string")
print("tigre")
# tigre
## print(variable)
animal = "tigre"
print (animal) # not any "" as animal is a variable
# tigre
## print(many "string")
# ==> if a comma is inserted between occurence (,) then a space will be included between them, and all on the same line
print("The itsy bitsy spider","climbed up","the waterspout.")
# The itsy bitsy spider climbed up the waterspout.
var = 1
account_balance = 1000.0
client_name = 'John Doe'
print(var, account_balance, client_name)
# 1 1000.0 John Doe
# ==> If no comma , NO space (like a concatenation with (+)) and all on the same line
print("Who" "are" "you")
# Whoareyou
## print("string" + var)
var = "3.7.1"
print("Python version: " + var)
# Python version: 3.7.1
john = 3
mary = 5
adam = 6
totalApples = john + mary + adam
print ("Total number of apples = ",totalApples)
# Total number of apples = 14
## \n
# \n = (n = new line) [RETURN KEY], goes to the next line, end = \n by default
print(" *\n * *\n * *\n * *\n*** ***\n * *\n * *\n *****")
# *
# * *
# * *
# * *
#*** ***
# * *
# * *
# *****
## \
# place before a character we want to ignore the default value/effect: ' " \ n
print("\"I'm\"\n\"\"learning\"\"\n\"\"\"Python\"\"\"")
# "I'm"
# ""learning""
# """Python"""
print(len("\\\\")) # ==> \\ = \, * 2
# 2
## Next line
# with print()
print("The itsy bitsy spider climbed up the waterspout.") # goes to next line by default (end = \n)
print() # --> pour sauter une ligne
print("Down came the rain and washed the spider out.")
# The itsy bitsy spider climbed up the waterspout.
#
# Down came the rain and washed the spider out.
# with \n
print("The itsy bitsy spider\n\n\n") # default \n + 3*(\n), so lets 3 lines free
print("Down came the rain")
# The itsy bitsy spider
#
#
#
# Down came the rain
print("The itsy bitsy spider\n\n") # default \n + 2*(\n), so lets 2 lines free
print("Down came the rain")
# The itsy bitsy spider
#
#
# Down came the rain
print("The itsy bitsy spider\n") # default \n + 1*(\n), so lets 1 line free
print("Down came the rain")
# The itsy bitsy spider
#
# Down came the rain
print("The itsy bitsy spider") # default \n
print("Down came the rain")
# The itsy bitsy spider
# Down came the rain
## " and '
print("Homer don't care")
# Homer don't care
print('Homer don't care')
# SyntaxError: invalid syntax
## end =
# default is end="\n"
print("My name is", "Python.", end=" ") # insert a space in place of default line return
print("Monty Python.")
# My name is Python. Monty Python.
for digit in "0165031806510":
if digit == "0":
print("X",end="")
else:
print(digit,end="")
# X165X318X651X
## sep =
# linked to the comma (,) separation role
# defaut sep = espace
print("My", "name", "is", "Monty", "Python.", sep="-")
# My-name-is-Monty-Python.
print("My", "name", "is", sep="_", end="*")
print("Monty", "Python.", sep="*", end="*\n")
# My_name_is*Monty*Python.*
# sep=None = DEFAULT " "
print("Hello","World",sep=None)
# Hello World
## print with .format
print ('hello {} {}'.format('prenom','nom')) # --> a brace pair by value to insert
# hello prenom nom
print ('hello {1} {0}'.format('prenom','nom')) # --> we can use indexation (starting at 0)
# hello nom prenom
print ('hello {p} {n}'.format(p='prenom',n='nom')) # --> we can use variable(s) name(s)
# hello prenom nom
# !! if any mismatch ==> IndexError
print ('hello {} {}'.format('prenom'))
# IndexError: Replacement index 1 out of range for positional args tuple
## Rounding
result = 100.0/777
print(result)
# 0.1287001287001287
print('the result is {r:1.4f}'.format(r=result)) # 4 numbers after comma
# the result is 0.1287
print('the result is {r:10.4f}'.format(r=result)) # 10 = number of character(s) ==> if needed filled with spaces on the left
# the result is 0.1287
## %(var)
x = 'String'
print ('Place my variable here: %s' %(x)) # %s = waits a variable ==> %(x) = put the x variable
# Place my variable here: String
y = 2
print ('Place my variable here: %s' %(y))
# Place my variable here: 2
propGC = (4500.0 + 2575.0)/14800
print ("GC proportion is %.2f" % propGC) # 2*float after the comma
# GC proportion is 0.48
print ('floating point number: %1.1f' %(x)) # limits the float number after the comma, and insert 0 ti fill in if necessary (ex 1.45 --> 1.450000)
# floating point number: 1.1
print ('floating point number: %1.9f' %(x))
# floating point number: 1.123456789
print ('floating point number: %.9f' %(x)) # .x or 0.1x or 1.1x outputs the same
# floating point number: 1.123456789
print ('floating point number: %0.9f' %(x))
# floating point number: 1.123456789
print ('floating point number: %25.9f' %(x)) # insert spaces to fill in the 25 characters space
# floating point number: 1.123456789
print ('First: {x} Second {y} Third: {x}'.format(x='inserted',y='two!')) # a variable may be used many times
# First: inserted Second two! Third: inserted
## input()
# to retrieve data from the user
# input() gives a STRING
print("Tell me anything...")
anything = input() # a prompt appears, waiting for input, could be sound or image
print("Hmm...", anything, "... Really?")
# Tell me anything...
# nada
# Hmm... nada ... Really?
# int() et float() allow to cpe with the default string return of input() function
anything = int(input("Enter a number: "))
something = anything ** 2
print(anything, "to the power of 2 is", something)
# Enter a number: 2
# 2 to the power of 2 is 4
anything = int(float("Enter a number: "))
something = anything ** 2
print(anything, "to the power of 2 is", something)
# Enter a number: 2
# 2 to the power of 2 is 4.0
# concatenation
fnam = input("May I have your first name, please? ")
lnam = input("May I have your last name, please? ")
print("Thank you.")
print("\nYour name is " + fnam + " " + lnam + ".")
# May I have your first name, please? benoit
# May I have your last name, please? frerot
# Thank you.
# Your name is benoit frerot.