Skip to content

Commit 17c3495

Browse files
committed
formatting
1 parent 6df54ab commit 17c3495

File tree

1 file changed

+49
-17
lines changed

1 file changed

+49
-17
lines changed

using-functions.md

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ Now we know how to make Python show text.
1010
>>>
1111
```
1212

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.
1416

1517
```py
1618
>>> print('Hello!')
1719
Hello!
1820
>>>
1921
```
2022

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 :)
2225

2326
```py
2427
>>> print("Hello World!")
@@ -34,11 +37,20 @@ But what exactly is print?
3437
>>>
3538
```
3639

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"`.
3844

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._
4050

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?
4254

4355
```py
4456
>>> x = print('hello')
@@ -48,26 +60,39 @@ None
4860
>>>
4961
```
5062

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:
5265

5366
- 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.
5671
- 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`.
5876
- x is now `None`.
5977

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.
6182

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:
6387

6488
```py
6589
>>> str(123)
6690
'123'
6791
>>>
6892
```
6993

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.
7196

7297
```py
7398
>>> print("Hello", "World!")
@@ -83,7 +108,8 @@ Unlike with `+`, the parameters don't need to be strings.
83108
>>>
84109
```
85110

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.
87113

88114
```py
89115
>>> x = input("Enter something:")
@@ -93,23 +119,29 @@ Enter something:hello
93119
>>>
94120
```
95121

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.
97126

98127
You may want to add a space after the `:`, like this:
99128

100129
```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
102131
Enter something: hello
103132
>>>
104133
```
105134

106135
## Storing code in files
107136

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.
109139

110140
![New File in IDLE](idle-new.png)
111141

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
144+
the user. You need to print it instead.
113145

114146
```py
115147
print("Hello.")

0 commit comments

Comments
 (0)