You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: using-functions.md
+49-17Lines changed: 49 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -10,15 +10,18 @@ Now we know how to make Python show text.
10
10
>>>
11
11
```
12
12
13
-
But that includes `''`. One way to show text to the user without `''` is with the print function. In Python, printing doesn't have anything to do with physical printers, it just means showing text on the screen.
13
+
But that includes `''`. One way to show text to the user without `''`
14
+
is with the print function. In Python, printing doesn't have anything
15
+
to do with physical printers, it just means showing text on the screen.
14
16
15
17
```py
16
18
>>>print('Hello!')
17
19
Hello!
18
20
>>>
19
21
```
20
22
21
-
Now we are ready for a classic example, which is also the first program in many tutorials :)
23
+
Now we are ready for a classic example, which is also the first program
24
+
in many tutorials :)
22
25
23
26
```py
24
27
>>>print("Hello World!")
@@ -34,11 +37,20 @@ But what exactly is print?
34
37
>>>
35
38
```
36
39
37
-
In Python 3, print is a function. Functions do something. They are used by typing their name _[*]_ and parenthesis. Inside the parenthesis, we can pass some parameters too. In `print("hello")` the function name is `print` and we give it one parameter, which is `"hello"`.
40
+
In Python 3, print is a function. Functions do something. They are used
41
+
by typing their name _[*]_ and parenthesis. Inside the parenthesis, we
42
+
can pass some parameters too. In `print("hello")` the function name is
43
+
`print` and we give it one parameter, which is `"hello"`.
38
44
39
-
_[*] Actually, a name of [a variable that points to the function](https://www.youtube.com/watch?v=_AEJHKGk9ns). Functions are also variables in Python. This means that `print_me_a_thingy = print` and then `print_me_a_thingy('hello world')` works just fine._
45
+
_[*] Actually, a name of
46
+
[a variable that points to the function](https://www.youtube.com/watch?v=_AEJHKGk9ns).
47
+
Functions are also variables in Python. This means that
48
+
`print_me_a_thingy = print` and then `print_me_a_thingy('hello world')`
49
+
works just fine._
40
50
41
-
Functions are often thoght of as difficult to understand, but they really are not. They just do something with the parameters they're given. But if we do `x = print('hello')`, what is x?
51
+
Functions are often thoght of as difficult to understand, but they
52
+
really are not. They just do something with the parameters they're
53
+
given. But if we do `x = print('hello')`, what is x?
42
54
43
55
```py
44
56
>>> x =print('hello')
@@ -48,26 +60,39 @@ None
48
60
>>>
49
61
```
50
62
51
-
So doing `x = print('hello')` set `x` to `None`. Here's what happened, explained in more detail:
63
+
So doing `x = print('hello')` set `x` to `None`. Here's what happened,
64
+
explained in more detail:
52
65
53
66
- In `x = print('hello')`, the right side is processed first.
54
-
-`print('hello')` calls the print function with the parameter `'hello'`.
55
-
- The function runs **immediately** when it's called. It shows the word hello.
67
+
-`print('hello')` calls the print function with the parameter
68
+
`'hello'`.
69
+
- The function runs **immediately** when it's called. It shows the word
70
+
hello.
56
71
- The print function returns `None`.
57
-
- Now the right side has been processed. `print('hello')` returned `None`, so we can imagine we have `None` instead of `print('hello')` there, and the assignment now looks like `x = None`.
72
+
- Now the right side has been processed. `print('hello')` returned
73
+
`None`, so we can imagine we have `None` instead of
74
+
`print('hello')` there, and the assignment now looks like
75
+
`x = None`.
58
76
- x is now `None`.
59
77
60
-
All functions need to return something. So does the print function, and that's why it returns `None`. Of course, `x = print('hello')` is useless compared to `print('hello')`, because the print function always returns `None` and we can do `x = None` without any printing.
78
+
All functions need to return something. So does the print function, and
79
+
that's why it returns None. Of course, `x = print('hello')` is useless
80
+
compared to `print('hello')`, because the print function always returns
81
+
`None` and we can do `x = None` without any printing.
61
82
62
-
Calling a function without assigning the return value to anything (e.g. `print('hello')`) simply throws away the return value. The interactive `>>>` prompt also echoes the return value back if it's not None:
83
+
Calling a function without assigning the return value to anything (e.g.
84
+
`print('hello')` instead of `x = print('hello')`) simply throws away
85
+
the return value. The interactive `>>>` prompt also echoes the return
86
+
value back if it's not None:
63
87
64
88
```py
65
89
>>>str(123)
66
90
'123'
67
91
>>>
68
92
```
69
93
70
-
You can also pass multiple parameters separated with commas. The print function will add spaces between them.
94
+
You can also pass multiple parameters separated with commas. The print
95
+
function will add spaces between them.
71
96
72
97
```py
73
98
>>>print("Hello", "World!")
@@ -83,7 +108,8 @@ Unlike with `+`, the parameters don't need to be strings.
83
108
>>>
84
109
```
85
110
86
-
Not all functions return None. The input function can be used for getting a string from the user.
111
+
Not all functions return None. The input function can be used for
112
+
getting a string from the user.
87
113
88
114
```py
89
115
>>> x =input("Enter something:")
@@ -93,23 +119,29 @@ Enter something:hello
93
119
>>>
94
120
```
95
121
96
-
`input("Enter something:")` showed the text `Enter something:` on the screen and waited for me to type something. I typed hello and pressed Enter. Then input returned the hello I typed as a string and it was assigned to x.
122
+
`input("Enter something:")` showed the text `Enter something:` on the
123
+
screen and waited for me to type something. I typed hello and pressed
124
+
Enter. Then input returned the hello I typed as a string and it was
125
+
assigned to x.
97
126
98
127
You may want to add a space after the `:`, like this:
99
128
100
129
```py
101
-
>>> x =input("Enter something: ") # now there's space between : and where I type
130
+
>>> x =input("Enter something: ") # now there's space between : and where i type
102
131
Enter something: hello
103
132
>>>
104
133
```
105
134
106
135
## Storing code in files
107
136
108
-
Now it's time to write some code into a file for the first time. In IDLE, go to File at top left and select New File, or just press Ctrl+N.
137
+
Now it's time to write some code into a file for the first time. In
138
+
IDLE, go to File at top left and select New File, or just press Ctrl+N.
109
139
110
140

111
141
112
-
Type something like this into the window that opens. When your code is in a file, adding `x` will not show the value of a variable called x to the user. You need to print it instead.
142
+
Type something like this into the window that opens. When your code is
143
+
in a file, adding `x` will not show the value of a variable called x to
0 commit comments