Skip to content

Commit e8585f2

Browse files
committed
Merge pull request #335 from hjwp/syntax-highlight-python-code
Syntax-highlight Python code blocks
2 parents 0399249 + a8e41a1 commit e8585f2

File tree

1 file changed

+69
-41
lines changed

1 file changed

+69
-41
lines changed

en/python_introduction/README.md

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,9 @@ This will put you back into the command prompt.
413413

414414
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:
415415

416-
print('Hello, Django girls!')
416+
```python
417+
print('Hello, Django girls!')
418+
```
417419

418420
Obviously, you're a pretty seasoned python developer now, so feel free to write some code that you've learned today.
419421

@@ -450,7 +452,9 @@ Lots of things in code should only be executed when given conditions are met. Th
450452

451453
Replace the code in your **python_intro.py** file to this:
452454

453-
if 3 > 2:
455+
```python
456+
if 3 > 2:
457+
```
454458

455459
If we saved this and ran it, we'd see an error like this:
456460

@@ -461,8 +465,10 @@ If we saved this and ran it, we'd see an error like this:
461465

462466
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:
463467

464-
if 3 > 2:
465-
print('It works!')
468+
```python
469+
if 3 > 2:
470+
print('It works!')
471+
```
466472

467473
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.
468474

@@ -475,10 +481,12 @@ Save it and give it another run:
475481

476482
In previous examples, code was executed only when the conditions were True. But Python also has `elif` and `else` statements:
477483

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+
if 5 > 2:
486+
print('5 is indeed greater than 2')
487+
else:
488+
print('5 is not greater than 2')
489+
```
482490

483491
When this was run it would print out:
484492

@@ -487,13 +495,15 @@ When this was run it would print out:
487495

488496
If 2 were a greater number than 5, then the second command would be executed. Easy, right? Let's see how `elif` works:
489497

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+
```
497507

498508
and executed:
499509

@@ -519,11 +529,13 @@ Remember functions like `len()` that you can execute in Python? Well, good news,
519529

520530
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:
521531

522-
def hi():
523-
print('Hi there!')
524-
print('How are you?')
532+
```python
533+
def hi():
534+
print('Hi there!')
535+
print('How are you?')
525536

526-
hi()
537+
hi()
538+
```
527539

528540
Okay, our first function is ready!
529541

@@ -537,19 +549,23 @@ Let's run this now and see what happens:
537549

538550
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:
539551

540-
def hi(name):
552+
```python
553+
def hi(name):
554+
```
541555

542556
As you can see, we now gave our function a parameter that we called `name`:
543557

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+
def hi(name):
560+
if name == 'Ola':
561+
print('Hi Ola!')
562+
elif name == 'Sonja':
563+
print('Hi Sonja!')
564+
else:
565+
print('Hi anonymous!')
551566

552-
hi()
567+
hi()
568+
```
553569

554570
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:
555571

@@ -563,7 +579,9 @@ Oops, an error. Luckily, Python gives us a pretty useful error message.
563579
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.
564580
Let's fix it at the bottom of the file:
565581

566-
hi("Ola")
582+
```python
583+
hi("Ola")
584+
```
567585

568586
and run it again:
569587

@@ -572,7 +590,9 @@ and run it again:
572590

573591
And if we change the name?
574592

575-
hi("Sonja")
593+
```python
594+
hi("Sonja")
595+
```
576596

577597
and run it:
578598

@@ -587,10 +607,12 @@ This is awesome, right? This way you don't have to repeat yourself every time yo
587607

588608
Let's do something smarter -- there are more names than two, and writing a condition for each would be hard, right?
589609

590-
def hi(name):
591-
print('Hi ' + name + '!')
610+
```python
611+
def hi(name):
612+
print('Hi ' + name + '!')
592613

593-
hi("Rachel")
614+
hi("Rachel")
615+
```
594616

595617
Let's call the code now:
596618

@@ -607,23 +629,29 @@ As we mentioned, programmers are lazy, they don't like to repeat themselves. Pro
607629

608630
Still remember lists? Let's do a list of girls:
609631

610-
girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You']
632+
```python
633+
girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You']
634+
```
611635

612636
We want to greet all of them by their name. We have the `hi` function to do that, so let's use it in a loop:
613637

614-
for name in girls:
638+
```python
639+
for name in girls:
640+
```
615641

616642
The ```for``` statement behaves similarly to the ```if``` statement, code below both of these need to be indented four spaces.
617643

618644
Here is the full code that will be in the file:
619645

620-
def hi(name):
621-
print('Hi ' + name + '!')
646+
```python
647+
def hi(name):
648+
print('Hi ' + name + '!')
622649

623-
girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You']
624-
for name in girls:
625-
hi(name)
626-
print('Next girl')
650+
girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You']
651+
for name in girls:
652+
hi(name)
653+
print('Next girl')
654+
```
627655

628656
and when we run it:
629657

0 commit comments

Comments
 (0)