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: en/python_introduction/README.md
+69-41Lines changed: 69 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -413,7 +413,9 @@ This will put you back into the command prompt.
413
413
414
414
Earlier, we picked out a code editor from the [code editor](../code_editor/README.md) section. We'll need to open the editor now and write some code into a new file:
415
415
416
-
print('Hello, Django girls!')
416
+
```python
417
+
print('Hello, Django girls!')
418
+
```
417
419
418
420
Obviously, you're a pretty seasoned python developer now, so feel free to write some code that you've learned today.
419
421
@@ -450,7 +452,9 @@ Lots of things in code should only be executed when given conditions are met. Th
450
452
451
453
Replace the code in your **python_intro.py** file to this:
452
454
453
-
if 3 > 2:
455
+
```python
456
+
if3>2:
457
+
```
454
458
455
459
If we saved this and ran it, we'd see an error like this:
456
460
@@ -461,8 +465,10 @@ If we saved this and ran it, we'd see an error like this:
461
465
462
466
Python expects us to give further instructions to it which are supposed to be executed if the condition `3 > 2` turns out to be true (or `True` for that matter). Let’s try to make Python print “It works!”. Change your code in your **python_intro.py** file to this:
463
467
464
-
if 3 > 2:
465
-
print('It works!')
468
+
```python
469
+
if3>2:
470
+
print('It works!')
471
+
```
466
472
467
473
Notice how we've indented the next line of code by 4 spaces? We need to do this so Python knows what code to run if the code results in true. You can do one space, but nearly all Python programmers do 4 to make things look neat. A single tab will also count as 4 spaces.
468
474
@@ -475,10 +481,12 @@ Save it and give it another run:
475
481
476
482
In previous examples, code was executed only when the conditions were True. But Python also has `elif` and `else` statements:
477
483
478
-
if 5 > 2:
479
-
print('5 is indeed greater than 2')
480
-
else:
481
-
print('5 is not greater than 2')
484
+
```python
485
+
if5>2:
486
+
print('5 is indeed greater than 2')
487
+
else:
488
+
print('5 is not greater than 2')
489
+
```
482
490
483
491
When this was run it would print out:
484
492
@@ -487,13 +495,15 @@ When this was run it would print out:
487
495
488
496
If 2 were a greater number than 5, then the second command would be executed. Easy, right? Let's see how `elif` works:
489
497
490
-
name = 'Sonja'
491
-
if name == 'Ola':
492
-
print('Hey Ola!')
493
-
elif name == 'Sonja':
494
-
print('Hey Sonja!')
495
-
else:
496
-
print('Hey anonymous!')
498
+
```python
499
+
name ='Sonja'
500
+
if name =='Ola':
501
+
print('Hey Ola!')
502
+
elif name =='Sonja':
503
+
print('Hey Sonja!')
504
+
else:
505
+
print('Hey anonymous!')
506
+
```
497
507
498
508
and executed:
499
509
@@ -519,11 +529,13 @@ Remember functions like `len()` that you can execute in Python? Well, good news,
519
529
520
530
A function is a sequence of instructions that Python should execute. Each function in Python starts with the keyword `def`, is given a name and can have some parameters. Let's start with an easy one. Replace the code in **python_intro.py** with the following:
521
531
522
-
def hi():
523
-
print('Hi there!')
524
-
print('How are you?')
532
+
```python
533
+
defhi():
534
+
print('Hi there!')
535
+
print('How are you?')
525
536
526
-
hi()
537
+
hi()
538
+
```
527
539
528
540
Okay, our first function is ready!
529
541
@@ -537,19 +549,23 @@ Let's run this now and see what happens:
537
549
538
550
That was easy! Let's build our first function with parameters. We will use the previous example - a function that says 'hi' to the person running it - with a name:
539
551
540
-
def hi(name):
552
+
```python
553
+
defhi(name):
554
+
```
541
555
542
556
As you can see, we now gave our function a parameter that we called `name`:
543
557
544
-
def hi(name):
545
-
if name == 'Ola':
546
-
print('Hi Ola!')
547
-
elif name == 'Sonja':
548
-
print('Hi Sonja!')
549
-
else:
550
-
print('Hi anonymous!')
558
+
```python
559
+
defhi(name):
560
+
if name =='Ola':
561
+
print('Hi Ola!')
562
+
elif name =='Sonja':
563
+
print('Hi Sonja!')
564
+
else:
565
+
print('Hi anonymous!')
551
566
552
-
hi()
567
+
hi()
568
+
```
553
569
554
570
As you can see, we needed to put two indents before the `print` function, because `if` needs to know what should happen when the condition is met. Let's see how it works now:
555
571
@@ -563,7 +579,9 @@ Oops, an error. Luckily, Python gives us a pretty useful error message.
563
579
It tells us that the function `hi()` (the one we defined) has one required argument (called `name`) and that we forgot to pass it when calling the function.
564
580
Let's fix it at the bottom of the file:
565
581
566
-
hi("Ola")
582
+
```python
583
+
hi("Ola")
584
+
```
567
585
568
586
and run it again:
569
587
@@ -572,7 +590,9 @@ and run it again:
572
590
573
591
And if we change the name?
574
592
575
-
hi("Sonja")
593
+
```python
594
+
hi("Sonja")
595
+
```
576
596
577
597
and run it:
578
598
@@ -587,10 +607,12 @@ This is awesome, right? This way you don't have to repeat yourself every time yo
587
607
588
608
Let's do something smarter -- there are more names than two, and writing a condition for each would be hard, right?
589
609
590
-
def hi(name):
591
-
print('Hi ' + name + '!')
610
+
```python
611
+
defhi(name):
612
+
print('Hi '+ name +'!')
592
613
593
-
hi("Rachel")
614
+
hi("Rachel")
615
+
```
594
616
595
617
Let's call the code now:
596
618
@@ -607,23 +629,29 @@ As we mentioned, programmers are lazy, they don't like to repeat themselves. Pro
0 commit comments