@@ -29,20 +29,31 @@ Hello World!
29
29
30
30
But what exactly is print?
31
31
32
+ ## What are functions?
33
+
34
+ Let's see what happens if we type ` print ` without the ` ('Hello') ` part.
35
+
32
36
``` py
33
37
>> > print
34
38
< built- in function print >
35
39
>> >
36
40
```
37
41
38
- In Python 3, print is a function. Functions do something when they are
42
+ We could also type ` print(print) ` , it would do the same thing. Python
43
+ replied to us with some information about print wrapped in ` <> ` .
44
+
45
+ As we can see, print is a function. Functions do something when they are
39
46
** called** by typing their name and parentheses. Inside the
40
47
parentheses, we can pass some arguments too. In ` print("hello") ` the
41
48
function is ` print ` and we give it one argument, which is ` "hello" ` .
42
49
43
- Functions are sometimes thoght of as difficult to understand, but they
44
- really are not. They just do something when they are called. But if we
45
- do ` x = print('hello') ` , what is x?
50
+ Functions are easy to understand, They simply ** do something when they
51
+ are called** . Functions run immediately when we call them, so the
52
+ text appears on the screen right away when we run ` print(something) ` .
53
+
54
+ ## Return values
55
+
56
+ If we do ` x = print('hello') ` , what is x?
46
57
47
58
``` py
48
59
>> > x = print (' hello' )
@@ -58,8 +69,7 @@ explained in more detail:
58
69
- In ` x = print('hello') ` , the right side is processed first.
59
70
- ` print('hello') ` calls the print function with the argument
60
71
` 'hello' ` .
61
- - The function runs ** immediately** when it's called. It shows the word
62
- hello.
72
+ - The function runs. It shows the word hello.
63
73
- The print function ** returns** None. All functions need to return
64
74
something, and print returns None because there's no need to return
65
75
anything else.
@@ -68,17 +78,46 @@ explained in more detail:
68
78
there, and the assignment now looks like ` x = None ` .
69
79
- x is now None.
70
80
81
+ Now we understand what a return value is. When we call the function,
82
+ Python "replaces" ` function(args) ` with whatever the function returns.
83
+
71
84
Calling a function without assigning the return value to anything (e.g.
72
85
` print('hello') ` instead of ` x = print('hello') ` ) simply throws away
73
- the return value. The interactive ` >>> ` prompt also echoes the return
74
- value back if it's not None.
86
+ the return value. The interactive ` >>> ` prompt doesn't echo the return
87
+ value back because it's None.
75
88
76
89
Of course, ` x = print('hello') ` is useless compared to ` print('hello') `
77
90
because the print function always returns None and we can do ` x = None `
78
91
without any printing.
79
92
93
+ Not all functions return None. The input function can be used for
94
+ getting a string from the user.
95
+
96
+ ``` py
97
+ >> > x = input (" Enter something:" )
98
+ Enter something:hello
99
+ >> > x
100
+ ' hello'
101
+ >> >
102
+ ```
103
+
104
+ ` input("Enter something:") ` showed the text ` Enter something: ` on the
105
+ screen and waited for me to type something. I typed hello and pressed
106
+ Enter. Then input returned the hello I typed as a string and it was
107
+ assigned to x.
108
+
109
+ Usually we want to add a space after the ` : ` , like this:
110
+
111
+ ``` py
112
+ >> > x = input (" Enter something: " ) # now there's space between : and where i type
113
+ Enter something: hello
114
+ >> >
115
+ ```
116
+
117
+ ## More about print
118
+
80
119
We can also print an empty line by calling print without any
81
- arguments:
120
+ arguments.
82
121
83
122
``` py
84
123
>> > print ()
97
136
```
98
137
99
138
If we want to print a backslash, we need to ** escape** it by typing
100
- two backslashes:
139
+ two backslashes.
101
140
102
141
[ comment ] : # ( For some reason, GitHub's syntax highlighting doesn't )
103
142
[ comment ] : # ( work here. )
@@ -123,37 +162,14 @@ Unlike with `+`, the arguments don't need to be strings.
123
162
>> >
124
163
```
125
164
126
- Not all functions return None. The input function can be used for
127
- getting a string from the user.
128
-
129
- ``` py
130
- >> > x = input (" Enter something:" )
131
- Enter something:hello
132
- >> > x
133
- ' hello'
134
- >> >
135
- ```
136
-
137
- ` input("Enter something:") ` showed the text ` Enter something: ` on the
138
- screen and waited for me to type something. I typed hello and pressed
139
- Enter. Then input returned the hello I typed as a string and it was
140
- assigned to x.
141
-
142
- Usually we want to add a space after the ` : ` , like this:
143
-
144
- ``` py
145
- >> > x = input (" Enter something: " ) # now there's space between : and where i type
146
- Enter something: hello
147
- >> >
148
- ```
149
-
150
165
## Summary
151
166
152
167
- ` function() ` calls a function without any arguments, and
153
168
` function(1, 2, 3) ` calls a function with 1, 2 and 3 as arguments.
154
- ` x = function() ` calls a function, and assigns the return value of
155
- the call to x.
156
169
- When a function is called, it does something and returns something.
170
+ - ` function(stuff) ` is "replaced" with the return value in the code
171
+ that called it. For example, ` x = function() ` calls a function, and
172
+ then does ` x = the_return_value ` and the return value ends up in x.
157
173
- Python comes with ` print ` and ` input ` . They are built-in functions.
158
174
159
175
***
0 commit comments