diff --git a/astro.config.mjs b/astro.config.mjs index 92f7182..3bd3e5c 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -19,6 +19,7 @@ export default defineConfig({ resolve: { alias: { '@config': '/src/config', + '@': new URL('./src', import.meta.url).pathname, }, }, }, diff --git a/src/components/Section.astro b/src/components/Section.astro new file mode 100644 index 0000000..43bade1 --- /dev/null +++ b/src/components/Section.astro @@ -0,0 +1,20 @@ +--- +const { title, id, collapse = false } = Astro.props; +--- + +
+ {title} +
+ +
+
+ + \ No newline at end of file diff --git a/src/components/Sidebar.astro b/src/components/Sidebar.astro index 5ee76c4..49928c4 100644 --- a/src/components/Sidebar.astro +++ b/src/components/Sidebar.astro @@ -19,8 +19,28 @@ import Carousel from '../components/Carousel.jsx'; @@ -29,9 +49,28 @@ import Carousel from '../components/Carousel.jsx'; macOS diff --git a/src/config/tutorial.ts b/src/config/tutorial.ts index 9709324..79f2875 100644 --- a/src/config/tutorial.ts +++ b/src/config/tutorial.ts @@ -1,2 +1,6 @@ export const PYTHON_VERSION = "3.12"; -export const DJANGO_VERSION = "5.2.8"; \ No newline at end of file +export const DJANGO_VERSION = "5.2.8"; +export const PYTHON_RELEASE = "3.12.3"; +export const PYTHON_MIN_VERSION = "3.10"; +export const PYTHON_MIN_RELEASE = "3.10.13"; +export const PA_PYTHON_VERSION = "3.10"; diff --git a/src/pages/en/linux/02_intro_to_command_line.mdx b/src/pages/en/linux/02_intro_to_command_line.mdx new file mode 100644 index 0000000..10690cc --- /dev/null +++ b/src/pages/en/linux/02_intro_to_command_line.mdx @@ -0,0 +1,251 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Introduction to the command-line interface +currentOS: linux +order: 2 +--- + +# Introduction to the command-line interface + +> For readers at home: this chapter is covered in the [Your new friend: Command Line](https://www.youtube.com/watch?v=jvZLWhkzX-8) video. + +It's exciting, right?! You'll write your first line of code in just a few minutes! :) + +__Let us introduce you to your first new friend: the command line!__ + +The following steps will show you how to use the black window all hackers use. It might look a bit scary at +first but really it's just a prompt waiting for commands from you. + +> **Note** Please note that throughout this book we use the terms 'directory' and 'folder' interchangeably +but they are one and the same thing. + +## What is the command line? + +The window, which is usually called the __command line__ or __command-line interface__, is a text-based +application for viewing, handling, and manipulating files on your computer. It's much like Windows Explorer +or Finder on the Mac, but without the graphical interface. Other names for the command line are: *cmd*, +*CLI*, *prompt*, *console* or *terminal*. + +## Open the command-line interface + +To start some experiments we need to open our command-line interface first. + +It's probably under Applications → Accessories → Terminal, or Applications → System → Terminal, but that may +depend on your system. If it's not there, you can try to Google it. :) + +You now should see a white or black window that is waiting for your commands. + +### The command-line Prompt + +If you're on Linux, you probably see a `$`, like this: + +`command-line` +``` +$ +``` + +Each command will be prepended by a `$` and one space, but you should not type it. Your computer will do it +for you. :) + +> Just a small note: in your case there may be something like `ola@Olas-PC:~ $` before the prompt +sign, and this is 100% OK. + +The part up to and including the `$` or the `>` is called the *command line prompt*, or *prompt* for short. +It prompts you to input something there. + +In the tutorial, when we want you to type in a command, we will include the `$` or `>`, and occasionally +more to the left. Ignore the left part and only type in the command, which starts after the prompt. + +## Your first command (YAY!) + +Let's start by typing this command: + +`command-line` +``` +$ whoami +``` + +And then hit `enter`. This is our result: + +`command-line` +``` +$ whoami +olasitarska +``` + +As you can see, the computer has just printed your username. Neat, huh? :) + +> Try to type each command; do not copy-paste. You'll remember more this way! + +## Basics + +Each operating system has a slightly different set of commands for the command line, so make sure to follow instructions for your operating system. + +If you make a typo, you can use the left and right arrow keys to move your cursor, backspace and delete to edit the command. Most command lines don't support using the mouse to move the cursor. + +Let's try this, shall we? + +### Current directory + +It'd be nice to know where are we now, right? Let's see. Type this command and hit `enter`: + +`command-line` +``` +$ pwd +/Users/olasitarska +``` + +> **Note:** 'pwd' stands for 'print working directory'. + +You'll probably see something similar on your machine. Once you open the command line you usually start at +your user's home directory. + +--- + +### Learn more about a command + +Many commands you can type at the command prompt have built-in help that you can display and read! For +example, to learn more about the current directory command: + +macOS and Linux have a `man` command, which gives you help on commands. Try `man pwd` and see what it says, +or put `man` before other commands to see their help. The output of `man` is normally paged. Use the space +bar to move to the next page, and `q` to quit looking at the help. + +### List files and directories + +So what's in it? It'd be cool to find out. Let's see: + +`command-line` +``` +$ ls +Applications +Desktop +Downloads +Music +... +``` + +### Change current directory + +Now, let's go to our Desktop directory: + +`command-line` +``` +$ cd Desktop +``` + +Note that the directory name "Desktop" might be translated to the language of your Linux account. If that's +the case, you'll need to replace `Desktop` with the translated name; for example, `Schreibtisch` for German. + +Check if it's really changed: + +`command-line` +``` +$ pwd +/Users/olasitarska/Desktop +``` + +Here it is! + +> PRO tip: if you type `cd D` and then hit `tab` on your keyboard, the command line will automatically fill +in the rest of the name so you can navigate faster. If there is more than one folder starting with "D", hit + the `tab` key twice to get a list of options. + +--- + +### Create directory + +How about creating a practice directory on your desktop? You can do it this way: + +`command-line` +``` +$ mkdir practice +``` + +`command-line` +``` +$ cd practice +$ mkdir test +$ ls +test +``` + +Congrats! :) + +--- + +### Clean up + +We don't want to leave a mess, so let's remove everything we did until that point. + +First, we need to get back to Desktop: + +`command-line` +``` +$ cd .. +``` + +Using `..` with the `cd` command will change your current directory to the parent directory (that is, the +directory that contains your current directory). + +Check where you are: + +`command-line` +``` +$ pwd +/Users/olasitarska/Desktop +``` + +Now time to delete the `practice` directory: + +> __Attention__: Deleting files using `del`, `rmdir` or `rm` is irrecoverable, meaning _the deleted files +will be gone forever_! So be very careful with this command. + +`command-line` +``` +$ rm -r practice +``` + +Done! To be sure it's actually deleted, let's check it: + +`command-line` +``` +$ ls +``` + +### Exit + +That's it for now! You can safely close the command line now. Let's do it the hacker way, alright? :) + +`command-line` +``` +$ exit +``` + +Cool, huh? :) + +## Summary + + Here is a summary of some useful commands: + +Command (Windows) | Command (Mac OS / Linux) | Description | Example +----------------- | ------------------------ | -------------------------- | --------------------------------------------- +exit | exit | close the window | **exit** +cd | cd | change directory | **cd test** +cd | pwd | show the current directory | **cd** (Windows) or **pwd** (Mac OS / Linux) +dir | ls | list directories/files | **dir** +copy | cp | copy file | **copy c:\test\test.txt c:\windows\test.txt** +move | mv | move file | **move c:\test\test.txt c:\windows\test.txt** +mkdir | mkdir | create a new directory | **mkdir testdirectory** +rmdir (or del) | rm | delete a file | **del c:\test\test.txt** +rmdir /S | rm -r | delete a directory | **rm -r testdirectory** +[CMD] /? | man [CMD] | get help for a command | **cd /?** (Windows) or **man cd** (Mac OS / Linux) + +These are just a very few of the commands you can run in your command line, but you're not going to use +anything more than that today. + +If you're curious, [ss64.com](https://ss64.com) contains a complete reference of commands for all operating systems. + +## Ready? + +Let's dive into Python! diff --git a/src/pages/en/linux/03_python_installation.mdx b/src/pages/en/linux/03_python_installation.mdx new file mode 100644 index 0000000..bbb960a --- /dev/null +++ b/src/pages/en/linux/03_python_installation.mdx @@ -0,0 +1,122 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Python Installation +currentOS: linux +order: 3 +--- + +import { DJANGO_VERSION } from '../../../config/tutorial'; +import { PYTHON_VERSION } from '../../../config/tutorial'; +import { PYTHON_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_VERSION } from '../../../config/tutorial'; +import Section from "@/components/Section.astro"; + +# Let’s start with Python + +We're finally here! + +But first, let us tell you what Python is. Python is a very popular programming language that can be used +for creating websites, games, scientific software, graphics, and much, much more. + +Python originated in the late 1980s and its main goal is to be readable by human beings (not only machines!). + This is why it looks simpler than other programming languages, but don't worry – Python is also really powerful! + +# Python installation + +> **Note** If you're using a Chromebook, skip this chapter and make sure you follow the [Chromebook Setup](/en/chromebook/) instructions. + +> **Note** If you already worked through the [installation steps](../installation/README.md), there's no need +to do this again – you can skip straight ahead to the next chapter! + +# Python installation + +> **Note** If you already worked through the [installation steps](../installation/README.md), there's no need +to do this again – you can skip straight ahead to the next chapter! + +> For readers at home: this chapter is covered in the [Installing Python & Code Editor](https://www.youtube.com/watch?v=pVTaqzKZCdA) video. + +> This section is based on a tutorial by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots) + +Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want +you to install the latest version of Python 3, so if you have any earlier version, you will need to upgrade +it. If you already have version { PYTHON_MIN_VERSION } or higher you should be fine. + +Please install normal Python as follows, even when you have Anaconda installed on your computer. + +It is very likely that you already have Python installed out of the box. To check if you have it installed +(and which version it is), open a console and type the following command: + +`command-line` +``` +$ python3 --version +Python 3.12.3 +``` + +If you have a different version of Python installed, at least { PYTHON_MIN_VERSION } (e.g. { PYTHON_MIN_RELEASE }), +then you don't have to upgrade. If you don't have Python installed, or if you want a different version, first check which Linux distribution you are using with the following command: + +`command-line` +``` +$ grep '^NAME=' /etc/os-release +``` + +Afterwards, depending on the result, follow one of the following installation guides below this section. + +
+ Type this command into your console: + +`command-line` +``` +$ sudo apt install python3 +``` +
+ +
+ Use this command in your console: + +`command-line` +``` +$ sudo dnf install python3 +``` + +If you're on older Fedora versions you might get an error that the command `dnf` is not found. In that case, +you need to use `yum` instead. + +
+ +
+ Use this command in your console: + +`command-line` +``` +$ sudo zypper install python3 +``` +
+ + +Verify the installation was successful by opening a command prompt and running the `python3` command: + +`command-line` +``` +$ python3 --version +Python 3.12.3 +``` +The version shown may be different from { PYTHON_RELEASE } -- it should match the version you installed. + +---- + +If you have any doubts, or if something went wrong and you have no idea what to do next, please ask your +coach! Sometimes things don't go smoothly and it's better to ask for help from someone with more experience. \ No newline at end of file diff --git a/src/pages/en/linux/05_python_introduction.mdx b/src/pages/en/linux/05_python_introduction.mdx new file mode 100644 index 0000000..88bd392 --- /dev/null +++ b/src/pages/en/linux/05_python_introduction.mdx @@ -0,0 +1,1112 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Python Introduction +currentOS: linux +order: 5 +--- +import ErrorIcon from "../../../components/ErrorIcon"; + +# Introduction to Python + +> Part of this chapter is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots). + +Let's write some code! + +## Python prompt + +> For readers at home: this part is covered in the [Python Basics: Integers, Strings, Lists, Variables and +Errors](https://www.youtube.com/watch?v=MO63L4s-20U) video. + +To start playing with Python, we need to open up a *command line* on your computer. You should already know +how to do that – you learned it in the [Intro to Command Line](../intro_to_command_line/README.md) chapter. + +Once you're ready, follow the instructions below. + +We want to open up a Python console, so type in `python` on Windows or `python3` on Mac OS/Linux and hit +`enter`. + +`command-line` +``` +$ python3 +Python 3.12.3 (...) +Type "help", "copyright", "credits" or "license" for more information. +>>> +``` + +## Your first Python command! + +After running the Python command, the prompt changed to `>>>`. For us this means that for now we may only +use commands in the Python language. You don't have to type in `>>>` – Python will do that for you. + +If you want to exit the Python console at any point, type `exit()` or use the shortcut `Ctrl + Z` for +Windows and `Ctrl + D` for Mac/Linux. Then you won't see `>>>` any longer. + +For now, we don't want to exit the Python console. We want to learn more about it. Let's start by typing +some math, like `2 + 3` and hitting `enter`. + +`command-line` +```python +>>> 2 + 3 +5 +``` + +Nice! See how the answer popped out? Python knows math! You could try other commands like: + +- `4 * 5` +- `5 - 1` +- `40 / 2` + +To perform exponential calculation, say 2 to the power 3, we type: +`command-line` +```python +>>> 2 ** 3 +8 +``` + +Have fun with this for a little while and then get back here. :) + +As you can see, Python is a great calculator. If you're wondering what else you can do… + +## Strings + +How about your name? Type your first name in quotes like this: + +`command-line` +```python +>>> "Ola" +'Ola' +``` + +You've now created your first string! It's a sequence of characters that can be processed by a computer. The +string must always begin and end with the same character. This may be single (`'`) or double (`"`) quotes +(there is no difference!) The quotes tell Python that what's inside of them is a string. + +Strings can be strung together. Try this: + +`command-line` +```python +>>> "Hi there " + "Ola" +'Hi there Ola' +``` + +You can also multiply strings with a number: + +`command-line` +```python +>>> "Ola" * 3 +'OlaOlaOla' +``` + +If you need to put an apostrophe inside your string, you have two ways to do it. + +Using double quotes: + +`command-line` +```python +>>> "Runnin' down the hill" +"Runnin' down the hill" +``` + +or escaping the apostrophe with a backslash (`\`): + +`command-line` +```python +>>> 'Runnin\' down the hill' +"Runnin' down the hill" +``` + +Nice, huh? To see your name in uppercase letters, type: + +`command-line` +```python +>>> "Ola".upper() +'OLA' +``` + +You just used the `upper` __method__ on your string! A method (like `upper()`) is a sequence of instructions +that Python has to perform on a given object (`"Ola"`) once you call it. + +If you want to know the number of letters contained in your name, there is a __function__ for that too! + +`command-line` +```python +>>> len("Ola") +3 +``` + +Wonder why sometimes you call functions with a `.` at the end of a string (like `"Ola".upper()`) and sometimes +you first call a function and place the string in parentheses? Well, in some cases, functions belong to +objects, like `upper()`, which can only be performed on strings. In this case, we call the function a +__method__. Other times, functions don't belong to anything specific and can be used on different types of +objects, just like `len()`. That's why we're giving `"Ola"` as a parameter to the `len` function. + +### Summary + +OK, enough of strings. So far you've learned about: + +- __the prompt__ – typing commands (code) into the Python prompt results in answers in Python +- __numbers and strings__ – in Python numbers are used for math and strings for text objects +- __operators__ – like `+` and `*`, combine values to produce a new one +- __functions__ – like `upper()` and `len()`, perform actions on objects. + +These are the basics of every programming language you learn. Ready for something harder? We bet you are! + +## Errors + +Let's try something new. Can we get the length of a number the same way we could find out the length of our +name? Type in `len(304023)` and hit `enter`: + + `command-line` +```python +>>> len(304023) +Traceback (most recent call last): + File "", line 1, in +TypeError: object of type 'int' has no len() +``` + +We got our first error! The icon is our way of giving you a heads up that the code you +are about to run won't work as expected. Making mistakes (even intentional ones) are an important part of learning! + + It says that objects of type "int" (integers, whole numbers) have no length. So what can we do now? Maybe + we can write our number as a string? Strings have a length, right? + +`command-line` +```python +>>> len(str(304023)) +6 +``` + +It worked! We used the `str` function inside of the `len` function. `str()` converts everything to strings. + +- The `str` function converts things into __strings__ +- The `int` function converts things into __integers__ + +> Important: we can convert numbers into text, but we can't necessarily convert text into numbers – what +would `int('hello')` be anyway? + +## Variables + +An important concept in programming is variables. A variable is nothing more than a name for something so +you can use it later. Programmers use these variables to store data, make their code more readable and so +they don't have to keep remembering what things are. + +Let's say we want to create a new variable called `name`: + +`command-line` +```python +>>> name = "Ola" +``` + +We type name equals Ola. + +As you've noticed, your program didn't return anything like it did before. So how do we know that the +variable actually exists? Enter `name` and hit `enter`: + +`command-line` +```python +>>> name +'Ola' +``` + +Yippee! Your first variable! :) You can always change what it refers to: + +`command-line` +```python +>>> name = "Sonja" +>>> name +'Sonja' +``` + +You can use it in functions too: + +`command-line` +```python +>>> len(name) +5 +``` + +Awesome, right? Now, variables can be anything – numbers too! Try this: + +`command-line` +```python +>>> a = 4 +>>> b = 6 +>>> a * b +24 +``` + +But what if we used the wrong name? Can you guess what would happen? Let's try! + + `command-line` +```python +>>> city = "Tokyo" +>>> ctiy +Traceback (most recent call last): + File "", line 1, in +NameError: name 'ctiy' is not defined +``` + +An error! As you can see, Python has different types of errors and this one is called a **NameError**. +Python will give you this error if you try to use a variable that hasn't been defined yet. If you encounter +this error later, check your code to see if you've mistyped any names. + +Play with this for a while and see what you can do! + + +## The print function + +Try this: + +`command-line` +```python +>>> name = 'Maria' +>>> name +'Maria' +>>> print(name) +Maria +``` + +When you just type `name`, the Python interpreter responds with the string *representation* of the variable +'name', which is the letters M-a-r-i-a, surrounded by single quotes, ''. When you say `print(name)`, Python +will "print" the contents of the variable to the screen, without the quotes, which is neater. + +As we'll see later, `print()` is also useful when we want to print things from inside functions, or when we +want to print things on multiple lines. + + +## Lists + +Beside strings and integers, Python has all sorts of different types of objects. Now we're going to introduce +one called __list__. Lists are exactly what you think they are: objects which are lists of other objects. :) + +Go ahead and create a list: + +`command-line` +```python +>>> [] +[] +``` + +Yes, this list is empty. Not very useful, right? Let's create a list of lottery numbers. We don't want to +repeat ourselves all the time, so we will put it in a variable, too: + +`command-line` +```python +>>> lottery = [3, 42, 12, 19, 30, 59] +``` + +All right, we have a list! What can we do with it? Let's see how many lottery numbers there are in a list. +Do you have any idea which function you should use for that? You know this already! + +`command-line` +```python +>>> len(lottery) +6 +``` + +Yes! `len()` can give you a number of objects in a list. Handy, right? Maybe we will sort it now: + +`command-line` +```python +>>> lottery.sort() +``` + +This doesn't return anything, it just changed the order in which the numbers appear in the list. Let's print it out again and see what happened: + +`command-line` +```python +>>> print(lottery) +[3, 12, 19, 30, 42, 59] +``` + +As you can see, the numbers in your list are now sorted from the lowest to highest value. Congrats! + +Maybe we want to reverse that order? Let's do that! + +`command-line` +```python +>>> lottery.reverse() +>>> print(lottery) +[59, 42, 30, 19, 12, 3] +``` + +If you want to add something to your list, you can do this by typing this command: + +`command-line` +```python +>>> lottery.append(199) +>>> print(lottery) +[59, 42, 30, 19, 12, 3, 199] +``` + +If you want to show only the first number, you can do this by using __indexes__. An index is the number that +says where in a list an item occurs. Programmers prefer to start counting at 0, so the first object in your +list is at index 0, the next one is at 1, and so on. Try this: + +`command-line` +```python +>>> print(lottery[0]) +59 +>>> print(lottery[1]) +42 +``` + +As you can see, you can access different objects in your list by using the list's name and the object's +index inside of square brackets. + +To delete something from your list you will need to use __indexes__ as we learned above and the `pop()` +method. Let's try an example and reinforce what we learned previously; we will be deleting the first number +of our list. + +`command-line` +```python +>>> print(lottery) +[59, 42, 30, 19, 12, 3, 199] +>>> print(lottery[0]) +59 +>>> lottery.pop(0) +59 +>>> print(lottery) +[42, 30, 19, 12, 3, 199] +``` + +That worked like a charm! + +For extra fun, try some other indexes: 6, 7, 1000, -1, -6 or -1000. See if you can predict the result before +trying the command. Do the results make sense? + +You can find a list of all available list methods in this chapter of the Python documentation: +https://docs.python.org/3/tutorial/datastructures.html + +## Dictionaries + +> For readers at home: this part is covered in the [Python Basics: Dictionaries](https://www.youtube.com/watch?v=ZX1CVvZLE6c) video. + +A dictionary is similar to a list, but you access values by looking up a key instead of a numeric index. A +key can be any string or number. The syntax to define an empty dictionary is: + +`command-line` +```python +>>> {} +{} +``` + +This shows that you just created an empty dictionary. Hurray! + +Now, try writing the following command (try substituting your own information, too): + +`command-line` +```python +>>> participant = {'name': 'Ola', 'country': 'Poland', 'favorite_numbers': [7, 42, 92]} +``` + +With this command, you just created a variable named `participant` with three key–value pairs: + +- The key `name` points to the value `'Ola'` (a `string` object), +- `country` points to `'Poland'` (another `string`), +- and `favorite_numbers` points to `[7, 42, 92]` (a `list` with three numbers in it). + +You can check the content of individual keys with this syntax: + +`command-line` +```python +>>> print(participant['name']) +Ola +``` + +See, it's similar to a list. But you don't need to remember the index – just the name. + +What happens if we ask Python the value of a key that doesn't exist? Can you guess? Let's try it and see! + + `command-line` +```python +>>> participant['age'] +Traceback (most recent call last): + File "", line 1, in +KeyError: 'age' +``` + +Look, another error! This one is a **KeyError**. Python is helpful and tells you that the key `'age'` +doesn't exist in this dictionary. + +When should you use a dictionary or a list? Well, that's a good point to ponder. Think about the answer +before looking at it in the next line. + +- Do you just need an ordered sequence of items? Go for a list. +- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a +dictionary. + +Like lists, using the `len()` function on the dictionaries returns the number of key–value pairs in the +dictionary. Go ahead and type in this command: + +`command-line` +```python +>>> len(participant) +3 +``` + +Dictionaries, like lists, are *mutable*, meaning that they can be changed after they are created. You can +add new key–value pairs to a dictionary after it is created, like this: + +`command-line` +```python +>>> participant['favorite_language'] = 'Python' +``` + +I hope it makes sense up to now. :) Ready for some more fun with dictionaries? Read on for some amazing things. + +You can use the `pop()` method to delete an item in the dictionary. Say, if you want to delete the entry +corresponding to the key `'favorite_numbers'`, type in the following command: + +`command-line` +```python +>>> participant.pop('favorite_numbers') +[7, 42, 92] +>>> participant +{'country': 'Poland', 'favorite_language': 'Python', 'name': 'Ola'} +``` + +As you can see from the output, the key–value pair corresponding to the 'favorite_numbers' key has been deleted. + +As well as this, you can also change a value associated with an already-created key in the dictionary. Type this: + +`command-line` +```python +>>> participant['country'] = 'Germany' +>>> participant +{'country': 'Germany', 'favorite_language': 'Python', 'name': 'Ola'} +``` + +As you can see, the value of the key `'country'` has been altered from `'Poland'` to `'Germany'`. :) +Exciting? Hurrah! You just learned another amazing thing. + +### Summary + +Awesome! You know a lot about programming now. In this last part you learned about: + +- __errors__ – you now know how to read and understand errors that show up if Python doesn't understand a +command you've given it +- __variables__ – names for objects that allow you to code more easily and to make your code more readable +- __lists__ – lists of objects stored in a particular order +- __dictionaries__ – objects stored as key–value pairs + +Excited for the next part? :) + +## Compare things + +> For readers at home: this part is covered in the [Python Basics: Comparisons](https://www.youtube.com/watch?v=7bzxqIKYgf4) video. + +A big part of programming involves comparing things. What's the easiest thing to compare? Numbers! Let's +see how that works: + +`command-line` +```python +>>> 5 > 2 +True +>>> 3 < 1 +False +>>> 5 > 2 * 2 +True +>>> 1 == 1 +True +>>> 5 != 2 +True +>>> len([1, 2, 3]) > len([4, 5]) +True +``` + +We gave Python some numbers to compare. As you can see, not only can Python compare numbers, but it can also +compare values of mathematical expressions like `2 * 2` and function results like the `2` returned by +`len([4, 5])`. Nice, huh? + +Do you wonder why we put two equal signs `==` next to each other to compare if numbers are equal? We use a +single `=` for assigning values to variables. You always, __always__ need to put two of them – `==` – if +you want to check if things are equal to each other. We can also state that things are unequal to each other. +For that, we use the symbol `!=`, as shown in the example above. + +Give Python two more tasks: + +`command-line` +```python +>>> 6 >= 12 / 2 +True +>>> 3 <= 2 +False +``` + +We've seen `>` and `<`, but what do `>=` and `<=` mean? Read them like this: + +- x `>` y means: x is greater than y +- x `<` y means: x is less than y +- x `<=` y means: x is less than or equal to y +- x `>=` y means: x is greater than or equal to y + +Awesome! Wanna do one more? Try this: + +`command-line` +```python +>>> 6 > 2 and 2 < 3 +True +>>> 3 > 2 and 2 < 1 +False +>>> 3 > 2 or 2 < 1 +True +``` + +You can give Python as many numbers to compare as you want, and it will give you an answer! Pretty smart, right? + +- __and__ – if you use the `and` operator, both comparisons have to be True in order for the whole command to be True +- __or__ – if you use the `or` operator, only one of the comparisons has to be True in order for the whole +command to be True + +Have you heard of the expression "comparing apples to oranges"? Let's try the Python equivalent: + + `command-line` +```python +>>> 1 > 'django' +Traceback (most recent call last): + File "", line 1, in +TypeError: '>' not supported between instances of 'int' and 'str' +``` + +Here you see that just like in the expression, Python is not able to compare a number (`int`) and a string (`str`). +Instead, it shows a **TypeError** and tells us the two types can't be compared together. + +## Boolean + +Incidentally, you just learned about a new type of object in Python. It's called __Boolean__. + +There are only two Boolean objects: +- True +- False + +But for Python to understand this, you need to always write it as 'True' (first letter uppercase, with the +rest of the letters lowercased). __true, TRUE, and tRUE won't work – only True is correct.__ (The same applies to 'False' as well.) + +Booleans can be variables, too! See here: + +`command-line` +```python +>>> a = True +>>> a +True +``` + +You can also do it this way: + +`command-line` +```python +>>> a = 2 > 5 +>>> a +False +``` + +Practice and have fun with Booleans by trying to run the following commands: + +- `True and True` +- `False and True` +- `True or 1 == 1` +- `1 != 2` + +Congrats! Booleans are one of the coolest features in programming, and you just learned how to use them! + +# Save it! + +> For readers at home: this part is covered in the [Python Basics: Saving files and "If" statement](https://www.youtube.com/watch?v=dOAg6QVAxyk) video. + + +So far we've been writing all our python code in the interpreter, which limits us to entering one line of +code at a time. Normal programs are saved in files and executed by our programming language __interpreter__ +or __compiler__. So far we've been running our programs one line at a time in the Python __interpreter__. +We're going to need more than one line of code for the next few tasks, so we'll quickly need to: + +- Exit the Python interpreter +- Open up our code editor of choice +- Save some code into a new python file +- Run it! + +To exit from the Python interpreter that we've been using, type the `exit()` function + +`command-line` +```python +>>> exit() +$ +``` + +This will put you back into the command prompt. + +Earlier, we picked out a code editor from the [code editor](/en/code_editor/) section. We'll need to open +the editor now and write some code into a new file (or if you're using a Chromebook, create a new file in +the cloud IDE and open the file, which will be in the included code editor): + +`editor` +```python +print('Hello, Django girls!') +``` + +Obviously, you're a pretty seasoned Python developer now, so feel free to write some code that you've learned today. + +Now we need to save the file and give it a descriptive name. Let's call the file **python_intro.py** and +save it to your desktop. We can name the file anything we want, but the important part here is to make sure +the file ends in __.py__. The __.py__ extension tells our operating system that this is a +**Python executable file** and Python can run it. + +> **Note** You should notice one of the coolest thing about code editors: colors! In the Python console, +everything was the same color; now you should see that the `print` function is a different color from the +string. This is called "syntax highlighting", and it's a really useful feature when coding. The color of +things will give you hints, such as unclosed strings or a typo in a keyword name (like the `def` in a +function, which we'll see below). This is one of the reasons we use a code editor. :) + + +With the file saved, it's time to run it! Using the skills you've learned in the command line section, use +the terminal to **change directories** to the desktop. + +On Linux, it will be like this: + +`command-line` +``` +$ cd ~/Desktop +``` + +(Remember that the word "Desktop" might be translated to your local language.) + +If you get stuck, ask for help. That's exactly what the coaches are here for! + +Now use Python to execute the code in the file like this: + +`command-line` +``` +$ python3 python_intro.py +Hello, Django girls! +``` + +Obviously, you're a pretty seasoned Python developer now, so feel free to write some code that you've learned today. + +Now we need to save the file and give it a descriptive name. Let's call the file **python_intro.py** and +save it to your desktop. We can name the file anything we want, but the important part here is to make sure +the file ends in __.py__. The __.py__ extension tells our operating system that this is a +**Python executable file** and Python can run it. + +> **Note** You should notice one of the coolest thing about code editors: colors! In the Python console, +everything was the same color; now you should see that the `print` function is a different color from the +string. This is called "syntax highlighting", and it's a really useful feature when coding. The color of +things will give you hints, such as unclosed strings or a typo in a keyword name (like the `def` in a +function, which we'll see below). This is one of the reasons we use a code editor. :) + + +With the file saved, it's time to run it! Using the skills you've learned in the command line section, use +the terminal to **change directories** to the desktop. + +On a Mac, the command will look something like this: + +`command-line` +``` +$ cd ~/Desktop +``` + +If you get stuck, ask for help. That's exactly what the coaches are here for! + +Now use Python to execute the code in the file like this: + +`command-line` +``` +$ python3 python_intro.py +Hello, Django girls! +``` + +Alright! You just ran your first Python program that was saved to a file. Feel awesome? + +You can now move on to an essential tool in programming: + +## If … elif … else + +Lots of things in code should be executed only when given conditions are met. That's why Python has +something called __if statements__. + +Replace the code in your **python_intro.py** file with this: + +`python_intro.py` +```python +if 3 > 2: +``` + +If we were to save and run this, we'd see an error like this: + + `command-line` +``` +$ python3 python_intro.py +File "python_intro.py", line 2 + ^ +SyntaxError: unexpected EOF while parsing +``` + +Python expects us to give further instructions to it which are 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: + +`python_intro.py` +```python +if 3 > 2: + print('It works!') +``` + +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 result is 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 as long as your text editor is set to do so. When you +made your choice, don't change it! If you already indented with 4 spaces, make any future indentation with +4 spaces, too - otherwise you may run into problems. + +Save it and give it another run: + +`command-line` +```python +$ python3 python_intro.py +It works! +``` + +Note: Remember that on Windows, 'python3' is not recognized as a command. From now on, replace 'python3' +with 'python' to execute the file. + +### What if a condition isn't True? + +In previous examples, code was executed only when the conditions were True. But Python also has `elif` and +`else` statements: + +`python_intro.py` +```python +if 5 > 2: + print('5 is indeed greater than 2') +else: + print('5 is not greater than 2') +``` + +When this is run it will print out: + +`command-line` +``` +$ python3 python_intro.py +5 is indeed greater than 2 +``` + +If 2 were a greater number than 5, then the second command would be executed. Let's see how `elif` works: + +`python_intro.py` +```python +name = 'Sonja' +if name == 'Ola': + print('Hey Ola!') +elif name == 'Sonja': + print('Hey Sonja!') +else: + print('Hey anonymous!') +``` + +and executed: + +`command-line` +``` +$ python3 python_intro.py +Hey Sonja! +``` + +See what happened there? `elif` lets you add extra conditions that run if the previous conditions fail. + +You can add as many `elif` statements as you like after your initial `if` statement. For example: + +`python_intro.py` +```python +volume = 57 +if volume < 20: + print("It's kinda quiet.") +elif 20 <= volume < 40: + print("It's nice for background music") +elif 40 <= volume < 60: + print("Perfect, I can hear all the details") +elif 60 <= volume < 80: + print("Nice for parties") +elif 80 <= volume < 100: + print("A bit loud!") +else: + print("My ears are hurting! :(") +``` + +Python runs through each test in sequence and prints: + +`command-line` +``` +$ python3 python_intro.py +Perfect, I can hear all the details +``` + +## Comments + +Comments are lines beginning with `#`. You can write whatever you want after the `#` and Python will ignore +it. Comments can make your code easier for other people to understand. + +Let's see how that looks: + +`python_intro.py` +```python +# Change the volume if it's too loud or too quiet +if volume < 20 or volume > 80: + volume = 50 + print("That's better!") +``` + +You don't need to write a comment for every line of code, but they are useful for explaining why your code +is doing something, or providing a summary when it's doing something complex. + + +### Summary + +In the last few exercises you learned about: + +- __comparing things__ – in Python you can compare things by using `>`, `>=`, `==`, `<=`, `<` and the `and`, `or` operators +- __Boolean__ – a type of object that can only have one of two values: `True` or `False` +- __Saving files__ – storing code in files so you can execute larger programs. +- __if … elif … else__ – statements that allow you to execute code only when certain conditions are met. +- __comments__ - lines that Python won't run which let you document your code + +Time for the last part of this chapter! + +## Your own functions! + +> For readers at home: this part is covered in the [Python Basics: Functions](https://www.youtube.com/watch?v=5owr-6suOl0) video. + +Remember functions like `len()` that you can execute in Python? Well, good news – you will learn how to +write your own functions now! + +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 give it a go. Replace the code in +**python_intro.py** with the following: + +`python_intro.py` +```python +def hi(): + print('Hi there!') + print('How are you?') + +hi() +``` + +Okay, our first function is ready! + +You may wonder why we've written the name of the function at the bottom of the file. When we write +`def hi():` and the indented lines following, this is us writing instructions for what the `hi()` function +should do. Python will read and remember these instructions, but won't run the function yet. To tell Python +we want to run the function, we have to call the function with `hi()`. Python reads the file and executes +it from top to bottom, so we have to define the function in the file before we call it. + +Let's run this now and see what happens: + +`command-line` +``` +$ python3 python_intro.py +Hi there! +How are you? +``` + +Note: if it didn't work, don't panic! The output will help you to figure why: +- If you get a `NameError`, that probably means you typed something wrong, so you should check that you +used the same name when creating the function with `def hi():` and when calling it with `hi()`. +- If you get an `IndentationError`, check that both of the `print` lines have the same whitespace at the +start of a line: python wants all the code inside the function to be neatly aligned. +- If there's no output at all, check that the last `hi()` *isn't* indented - if it is, that line will +become part of the function too, and it will never get run. + +Let's build our first function with parameters. We will change the previous example – a function that says +'hi' to the person running it – with a name: + +`python_intro.py` +```python +def hi(name): +``` + +As you can see, we now gave our function a parameter that we called `name`: + +`python_intro.py` +```python +def hi(name): + if name == 'Ola': + print('Hi Ola!') + elif name == 'Sonja': + print('Hi Sonja!') + else: + print('Hi anonymous!') + +hi() +``` + +Remember: The `print` function is indented four spaces within the `if` statement. This is because the +function runs when the condition is met. Let's see how it works now: + + `command-line` +``` +$ python3 python_intro.py +Traceback (most recent call last): +File "python_intro.py", line 10, in + hi() +TypeError: hi() missing 1 required positional argument: 'name' +``` + +Oops, an error. Luckily, Python gives us a pretty useful error message. +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. Let's fix it at the bottom of the file: + +1python_intro.py` +```python +hi("Ola") +``` + +And run it again: + +`command-line` +``` +$ python3 python_intro.py +Hi Ola! +``` + +And if we change the name? + +`python_intro.py` +```python +hi("Sonja") +``` + +And run it: + +`command-line` +``` +$ python3 python_intro.py +Hi Sonja! +``` + +Now, what do you think will happen if you write another name in there? (Not Ola or Sonja.) Give it a try +and see if you're right. It should print out this: + +`command-line` +``` +Hi anonymous! +``` + +This is awesome, right? This way you don't have to repeat yourself every time you want to change the name +of the person the function is supposed to greet. And that's exactly why we need functions – you never want +to repeat your code! + +Let's do something smarter – there are more names than two, and writing a condition for each would be hard, +right? Replace the content of your file with the following: + +`python_intro.py` +```python +def hi(name): + print('Hi ' + name + '!') + +hi("Rachel") +``` + +Let's call the code now: + +`command-line` +``` +$ python3 python_intro.py +Hi Rachel! +``` + +Congratulations! You just learned how to write functions! :) + +## Loops + +> For readers at home: this part is covered in the [Python Basics: For Loop](https://www.youtube.com/watch?v=aEA6Rc86HF0) video. + +This is the last part already. That was quick, right? :) + +Programmers don't like to repeat themselves. Programming is all about automating things, so we don't want +to greet every person by their name manually, right? That's where loops come in handy. + +Still remember lists? Let's do a list of girls: + +`python_intro.py` +```python +girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You'] +``` + +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: + +`python_intro.py` +```python +for name in girls: +``` + +The `for` statement behaves similarly to the `if` statement; code below both of these need to be indented four spaces. + +Here is the full code that will be in the file: + +`python_intro.py` +```python +def hi(name): + print('Hi ' + name + '!') + +girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You'] +for name in girls: + hi(name) + print('Next girl') +``` + +And when we run it: + +`command-line` +``` +$ python3 python_intro.py +Hi Rachel! +Next girl +Hi Monica! +Next girl +Hi Phoebe! +Next girl +Hi Ola! +Next girl +Hi You! +Next girl +``` + +As you can see, everything you put inside a `for` statement with an indent will be repeated for every +element of the list `girls`. + +You can also use `for` on numbers using the `range` function: + +`python_intro.py` +```python +for i in range(1, 6): + print(i) +``` + +Which would print: + +`command-line` +``` +1 +2 +3 +4 +5 +``` + +`range` is a function that creates a list of numbers following one after the other (these numbers are +provided by you as parameters). + +Note that the second of these two numbers is not included in the list that is output by Python (meaning +`range(1, 6)` counts from 1 to 5, but does not include the number 6). That is because `range` is half-open, and by that we mean it includes the first value, but not the last. + +## Summary + +That's it. __You totally rock!__ This was a tricky chapter, so you should feel proud of yourself. We're +definitely proud of you for making it this far! + +For official and full python tutorial visit https://docs.python.org/3/tutorial/. This will give you a more +thorough and complete study of the language. Cheers! :) + +You might want to briefly do something else – stretch, walk around for a bit, rest your eyes – before going +on to the next chapter. :) + + +![Cupcake](/images/cupcake.png) diff --git a/src/pages/en/linux/07_django_installation.mdx b/src/pages/en/linux/07_django_installation.mdx new file mode 100644 index 0000000..955291a --- /dev/null +++ b/src/pages/en/linux/07_django_installation.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django Installation +currentOS: linux +order: 7 +--- \ No newline at end of file diff --git a/src/pages/en/linux/08_django_start_project.mdx b/src/pages/en/linux/08_django_start_project.mdx new file mode 100644 index 0000000..b31ac79 --- /dev/null +++ b/src/pages/en/linux/08_django_start_project.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Your first Django project! +currentOS: linux +order: 8 +--- \ No newline at end of file diff --git a/src/pages/en/linux/09_django_models.mdx b/src/pages/en/linux/09_django_models.mdx new file mode 100644 index 0000000..6592b4b --- /dev/null +++ b/src/pages/en/linux/09_django_models.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django Models +currentOS: linux +order: 9 +--- \ No newline at end of file diff --git a/src/pages/en/linux/10_django_admin.mdx b/src/pages/en/linux/10_django_admin.mdx new file mode 100644 index 0000000..84cb855 --- /dev/null +++ b/src/pages/en/linux/10_django_admin.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django admin +currentOS: linux +order: 10 +--- \ No newline at end of file diff --git a/src/pages/en/linux/11_deploy.mdx b/src/pages/en/linux/11_deploy.mdx new file mode 100644 index 0000000..7a0bc1f --- /dev/null +++ b/src/pages/en/linux/11_deploy.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Deploy! +currentOS: linux +order: 11 +--- \ No newline at end of file diff --git a/src/pages/en/linux/index.md b/src/pages/en/linux/index.md deleted file mode 100644 index 0f177e2..0000000 --- a/src/pages/en/linux/index.md +++ /dev/null @@ -1,18 +0,0 @@ ---- -layout: ../../../layouts/TutorialLayout.astro -title: Django Girls Tutorial (Linux) -currentOS: linux ---- - -# Welcome to the Django Girls Tutorial for Linux - -This version of the tutorial is tailored for Linux users. -We’ll cover installation using package managers and walk through Django setup on popular distributions. - -## What You’ll Learn -- Installing Python and pip on Linux -- Setting up Django -- Creating your first Django project -- Building and deploying your app - -👉 Ready? Start with [Chapter 1](/linux/chapter1/). \ No newline at end of file diff --git a/src/pages/en/linux/index.mdx b/src/pages/en/linux/index.mdx new file mode 100644 index 0000000..5fda87c --- /dev/null +++ b/src/pages/en/linux/index.mdx @@ -0,0 +1,358 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django Girls Tutorial (Linux) +currentOS: linux +--- + +import { DJANGO_VERSION } from '../../../config/tutorial'; +import { PYTHON_VERSION } from '../../../config/tutorial'; +import { PYTHON_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_VERSION } from '../../../config/tutorial'; +import Section from "@/components/Section.astro"; + +# Installation + +To install software on your machine, follow the instructions below: + +## Brief intro to the command line + +Many of the steps below reference the "console", "terminal", "command window", or "command line" -- these all + mean the same thing: a window on your computer where you can enter commands. When you get to the main tutorial, + you'll learn more about the command line. For now, the main thing you need to know is how to open a command + window and what it looks like: + +It's probably under Applications → Accessories → Terminal, or Applications → System → Terminal, but that may +depend on your system. If it's not there, you can try to Google it. :) + +### The Command-line Prompt + +Now you know how to open a command line, we just need to understand what the "prompt" is. + +If you're on Linux, you probably see a `$`, like this: + +`command-line` +``` +$ +``` + +Each command will be prepended by a `$` and one space, but you should not type it. Your computer will do it for you. :) + +> Just a small note: in your case there may be something like `ola@Olas-PC:~ $` before the prompt sign, +and this is 100% OK. + +The part up to and including the `$` is called the *command line prompt*, or *prompt* for short. It prompts +you to input something there. + +In the tutorial, when we want you to type in a command, we will include the `$`, and occasionally more to the +left. Ignore the left part and only type in the command, which starts after the prompt. + +## Install Python +> For readers at home: this chapter is covered in the [Installing Python & Code Editor](https://www.youtube.com/watch?v=pVTaqzKZCdA) video. + +> This section is based on a tutorial by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots) + +Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want +you to install the latest version of Python 3, so if you have any earlier version, you will need to upgrade +it. If you already have version { PYTHON_MIN_VERSION } or higher you should be fine. + +Please install normal Python as follows, even when you have Anaconda installed on your computer. + +It is very likely that you already have Python installed out of the box. To check if you have it installed +(and which version it is), open a console and type the following command: + +`command-line` +``` +$ python3 --version +Python 3.12.3 +``` + +If you have a different version of Python installed, at least { PYTHON_MIN_VERSION } (e.g. { PYTHON_MIN_RELEASE }), +then you don't have to upgrade. If you don't have Python installed, or if you want a different version, first check which Linux distribution you are using with the following command: + +`command-line` +``` +$ grep '^NAME=' /etc/os-release +``` + +Afterwards, depending on the result, follow one of the following installation guides below this section. + +
+ Type this command into your console: + +`command-line` +``` +$ sudo apt install python3 +``` +
+ +
+ Use this command in your console: + +`command-line` +``` +$ sudo dnf install python3 +``` + +If you're on older Fedora versions you might get an error that the command `dnf` is not found. In that case, +you need to use `yum` instead. + +
+ +
+ Use this command in your console: + +`command-line` +``` +$ sudo zypper install python3 +``` +
+ + +Verify the installation was successful by opening a command prompt and running the `python3` command: + +`command-line` +``` +$ python3 --version +Python 3.12.3 +``` +The version shown may be different from { PYTHON_RELEASE } -- it should match the version you installed. + +---- + +If you have any doubts, or if something went wrong and you have no idea what to do next, please ask your +coach! Sometimes things don't go smoothly and it's better to ask for help from someone with more experience. + + +## Install a code editor +There are a lot of different editors and it largely boils down to personal preference. Most Python programmers use complex but extremely powerful IDEs (Integrated Development Environments), such as PyCharm. As a beginner, however, that's probably less suitable; our recommendations are equally powerful, but a lot simpler. + +Our suggestions are below, but feel free to ask your coach what their preferences are – it'll be easier to get help from them. + +## Visual Studio Code + +Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and macOS. It includes support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring. + +[Download it here](https://code.visualstudio.com/) + +## Gedit + +Gedit is an open-source, free editor, available for all operating systems. + +[Download it here](https://wiki.gnome.org/Apps/Gedit#Download) + +## Sublime Text + +Sublime Text is a very popular editor with a free evaluation period and it's available for all operating systems. + +[Download it here](https://www.sublimetext.com/) + +## Why are we installing a code editor? + +You might be wondering why we are installing this special code editor software, rather than using something +like Word or Notepad. + +The first reason is that code needs to be **plain text**, and the problem with programs like Word and Textedit +is that they don't actually produce plain text, they produce rich text (with fonts and formatting), using +custom formats like [RTF (Rich Text Format)](https://en.wikipedia.org/wiki/Rich_Text_Format). + +The second reason is that code editors are specialized for editing code, so they can provide helpful features +like highlighting code with color according to its meaning, or automatically closing quotes for you. + +We'll see all this in action later. Soon, you'll come to think of your trusty old code editor as one of your +favorite tools. :) + + +## Set up virtualenv and install Django +> Part of this section is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots). + +> Part of this section is based on the [django-marcador +tutorial](http://django-marcador.keimlink.de/) licensed under the Creative Commons +Attribution-ShareAlike 4.0 International License. The django-marcador tutorial +is copyrighted by Markus Zapke-Gründemann et al. + + +## Virtual environment + +Before we install Django we will get you to install an extremely useful tool to help keep your coding environment tidy on your computer. It's possible to skip this step, but it's highly recommended to follow it. Starting with the best possible setup will save you a lot of trouble in the future! + +So, let's create a **virtual environment** (also called a *virtualenv*). Virtualenv will isolate your Python/Django setup on a per-project basis. This means that any changes you make to one website won't affect any others you're also developing. Neat, right? + +All you need to do is find a directory in which you want to create the `virtualenv`; your home directory, for example. On Windows, it might look like `C:\Users\Name\` (where `Name` is the name of your login). + +For this tutorial we will be using a new directory `djangogirls` from your home directory: + +`command-line` +``` +$ mkdir djangogirls +$ cd djangogirls +``` + +We will make a virtualenv called `myvenv`. The general command will be in the format: + +`command-line` +``` +$ python3 -m venv myvenv +``` + +We can create a `virtualenv` on Linux by running `python3 -m venv myvenv`. +It will look like this: + +`command-line` +``` +$ python3 -m venv myvenv +``` + +`myvenv` is the name of your `virtualenv`. You can use any other name, but stick to lowercase and use no +spaces. It is also a good idea to keep the name short as you'll be referencing it a lot! + +> __NOTE:__ On some versions of Debian/Ubuntu you may receive the following error: + +`command-line` +``` +The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu +systems, you need to install the python3-venv package using the following command. + apt install python3-venv +You may need to use sudo with that command. After installing the python3-venv package, recreate your +virtual environment. +``` + +In this case, follow the instructions above and install the `python3-venv` package: +`command-line` +``` +$ sudo apt install python3-venv +``` + +> __NOTE:__ On some versions of Debian/Ubuntu initiating the virtual environment like this currently gives +the following error: + +`command-line` +``` +>Error: Command '['/home/eddie/Slask/tmp/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', +'--default-pip']' returned non-zero exit status 1 +``` + +To get around this, use the `virtualenv` command instead. + +`command-line` +``` +$ sudo apt install python-virtualenv +$ virtualenv --python=python3.12 myvenv +``` + +> __NOTE:__ If you get an error like + +`command-line` +``` +E: Unable to locate package python3-venv +``` + +then instead run: + +`command-line` +``` +sudo apt install python3.12-venv +``` + +## Install Git +Git is a "version control system" used by a lot of programmers. This software can track changes to files +over time so that you can recall specific versions later. A bit like the "track changes" feature in word +processor programs (e.g., Microsoft Word or LibreOffice Writer), but much more powerful. + +## Installing Git +
+`command-line` +```bash +$ sudo apt install git +``` +
+ +
+`command-line` +```bash +$ sudo dnf install git +``` +
+ +
+`command-line` +```bash +$ sudo zypper install git +``` +
+ + +## Create a GitHub account +Go to [GitHub.com](https://www.github.com) and sign up for a new, free user account. Be sure to remember +your password (add it to your password manager, if you use one). + +## Create a PythonAnywhere account +PythonAnywhere is a service for running Python code on servers "in the cloud". We'll use +it for hosting our site, live and on the Internet. + +We will be hosting the blog we're building on PythonAnywhere. Sign up for a "Beginner" account on +PythonAnywhere (the free tier is fine, you don't need a credit card). + +* [www.pythonanywhere.com](https://www.pythonanywhere.com/) + +![The PythonAnywhere signup page showing button to create a free 'Beginner' account](/images/pythonanywhere_beginner_account_button.png) + +> **Note** When choosing your username here, bear in mind that your blog's URL will take the form +`yourusername.pythonanywhere.com`, so choose either your own nickname or a name for what your blog is all +about. Also, be sure to remember your password (add it to your password manager, if you use one). + + +## Creating a PythonAnywhere API token + +This is something you only need to do once. When you've signed up for PythonAnywhere, you'll be taken to +your dashboard. Find the link near the top right to your "Account" page: + +![Account link on the top right on the page](/images/pythonanywhere_account.png) + +then select the tab named "API token", and hit the button that says "Create new API token". + +![The API token tab on the Account page](/images/pythonanywhere_create_api_token.png) + + +# Start reading + +Congratulations, you are all set up and ready to go! If you still have some time before the workshop, it would +be useful to start reading a few of the beginning chapters: + +* [How the internet works](/en/common/01-how-the-internet-works/) + +* [Introduction to the command line](/en/linux/02-intro_to_command_line/) + +* [Introduction to Python](/en/linux/03-python_introduction/) + +* [What is Django?](/en/common/06_django/) + + +# Enjoy the workshop! + +When you begin the workshop, you'll be able to go straight to [Your first Django project!](/en/linux/08_django_start_project/) because you already covered the material in the earlier chapters. + diff --git a/src/pages/en/macos/02_intro_to_command_line.mdx b/src/pages/en/macos/02_intro_to_command_line.mdx new file mode 100644 index 0000000..d507159 --- /dev/null +++ b/src/pages/en/macos/02_intro_to_command_line.mdx @@ -0,0 +1,262 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Introduction to the command-line interface +currentOS: macos +order: 2 +--- + +# Introduction to the command-line interface + +> For readers at home: this chapter is covered in the [Your new friend: Command Line](https://www.youtube.com/watch?v=jvZLWhkzX-8) video. + +It's exciting, right?! You'll write your first line of code in just a few minutes! :) + +__Let us introduce you to your first new friend: the command line!__ + +The following steps will show you how to use the black window all hackers use. It might look a bit scary at +first but really it's just a prompt waiting for commands from you. + +> **Note** Please note that throughout this book we use the terms 'directory' and 'folder' interchangeably +but they are one and the same thing. + +## What is the command line? + +The window, which is usually called the __command line__ or __command-line interface__, is a text-based +application for viewing, handling, and manipulating files on your computer. It's much like Windows Explorer +or Finder on the Mac, but without the graphical interface. Other names for the command line are: *cmd*, +*CLI*, *prompt*, *console* or *terminal*. + +## Open the command-line interface + +To start some experiments we need to open our command-line interface first. + +Go to Launchpad → Other → Terminal. + +You now should see a white or black window that is waiting for your commands. + +### The command-line Prompt + +If you're on Mac, you probably see a `$`, like this: + +`command-line` +``` +$ +``` + +Each command will be prepended by a `$` or `>` and one space, but you should not type it. Your computer will + do it for you. :) + +> Just a small note: in your case there may be something like `Olas-MacBook-Air:~ ola$` before the prompt +sign, and this is 100% OK. + +The part up to and including the `$` or the `>` is called the *command line prompt*, or *prompt* for short. +It prompts you to input something there. + +In the tutorial, when we want you to type in a command, we will include the `$`, and occasionally +more to the left. Ignore the left part and only type in the command, which starts after the prompt. + +## Your first command (YAY!) + +Let's start by typing this command: + +`command-line` +``` +$ whoami +``` + +And then hit `enter`. This is our result: + +`command-line` +``` +$ whoami +olasitarska +``` + +As you can see, the computer has just printed your username. Neat, huh? :) + +> Try to type each command; do not copy-paste. You'll remember more this way! + +## Basics + +Each operating system has a slightly different set of commands for the command line, so make sure to follow instructions for your operating system. + +If you make a typo, you can use the left and right arrow keys to move your cursor, backspace and delete to edit the command. Most command lines don't support using the mouse to move the cursor. + +Let's try this, shall we? + +### Current directory + +It'd be nice to know where are we now, right? Let's see. Type this command and hit `enter`: + +`command-line` +``` +$ pwd +/Users/olasitarska +``` + +> **Note:** 'pwd' stands for 'print working directory'. + +You'll probably see something similar on your machine. Once you open the command line you usually start at +your user's home directory. + +--- + +### Learn more about a command + +Many commands you can type at the command prompt have built-in help that you can display and read! For +example, to learn more about the current directory command: + +macOS and Linux have a `man` command, which gives you help on commands. Try `man pwd` and see what it says, +or put `man` before other commands to see their help. The output of `man` is normally paged. Use the space +bar to move to the next page, and `q` to quit looking at the help. + +### List files and directories + +So what's in it? It'd be cool to find out. Let's see: + +`command-line` +``` +$ ls +Applications +Desktop +Downloads +Music +... +``` + +### Change current directory + +Now, let's go to our Desktop directory: + +`command-line` +``` +$ cd Desktop +``` + +Check if it's really changed: + +`command-line` +``` +$ pwd +/Users/olasitarska/Desktop +``` + +Here it is! + +> PRO tip: if you type `cd D` and then hit `tab` on your keyboard, the command line will automatically fill +in the rest of the name so you can navigate faster. If there is more than one folder starting with "D", hit +the `tab` key twice to get a list of options. + +--- + +### Create directory + +How about creating a practice directory on your desktop? You can do it this way: + +`command-line` +``` +$ mkdir practice +``` + +This little command will create a folder with the name `practice` on your desktop. You can check if it's +there by looking on your Desktop or by running a `ls` or `dir` command! Try it. :) + +> PRO tip: If you don't want to type the same commands over and over, try pressing the `up arrow` and +`down arrow` on your keyboard to cycle through recently used commands. + +--- + +### Exercise! + +A small challenge for you: in your newly created `practice` directory, create a directory called `test`. +(Use the `cd` and `mkdir` commands.) + +#### Solution: + +`command-line` +``` +$ cd practice +$ mkdir test +$ ls +test +``` + +Congrats! :) + +--- + +### Clean up + +We don't want to leave a mess, so let's remove everything we did until that point. + +First, we need to get back to Desktop: + +`command-line` +``` +$ cd .. +``` + +Using `..` with the `cd` command will change your current directory to the parent directory (that is, the +directory that contains your current directory). + +Check where you are: + +`command-line` +``` +$ pwd +/Users/olasitarska/Desktop +``` + +Now time to delete the `practice` directory: + +> __Attention__: Deleting files using `del`, `rmdir` or `rm` is irrecoverable, meaning _the deleted files +will be gone forever_! So be very careful with this command. + +`command-line` +``` +$ rm -r practice +``` + +Done! To be sure it's actually deleted, let's check it: + +`command-line` +``` +$ ls +``` + +### Exit + +That's it for now! You can safely close the command line now. Let's do it the hacker way, alright? :) + +`command-line` +``` +$ exit +``` + +Cool, huh? :) + +## Summary + + Here is a summary of some useful commands: + +Command (Windows) | Command (Mac OS / Linux) | Description | Example +----------------- | ------------------------ | -------------------------- | --------------------------------------------- +exit | exit | close the window | **exit** +cd | cd | change directory | **cd test** +cd | pwd | show the current directory | **cd** (Windows) or **pwd** (Mac OS / Linux) +dir | ls | list directories/files | **dir** +copy | cp | copy file | **copy c:\test\test.txt c:\windows\test.txt** +move | mv | move file | **move c:\test\test.txt c:\windows\test.txt** +mkdir | mkdir | create a new directory | **mkdir testdirectory** +rmdir (or del) | rm | delete a file | **del c:\test\test.txt** +rmdir /S | rm -r | delete a directory | **rm -r testdirectory** +[CMD] /? | man [CMD] | get help for a command | **cd /?** (Windows) or **man cd** (Mac OS / Linux) + +These are just a very few of the commands you can run in your command line, but you're not going to use +anything more than that today. + +If you're curious, [ss64.com](https://ss64.com) contains a complete reference of commands for all operating systems. + +## Ready? + +Let's dive into Python! diff --git a/src/pages/en/macos/03_python_installation.mdx b/src/pages/en/macos/03_python_installation.mdx new file mode 100644 index 0000000..435a32c --- /dev/null +++ b/src/pages/en/macos/03_python_installation.mdx @@ -0,0 +1,59 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Python Installation +currentOS: macos +order: 3 +--- + +import { DJANGO_VERSION } from '../../../config/tutorial'; +import { PYTHON_VERSION } from '../../../config/tutorial'; +import { PYTHON_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_VERSION } from '../../../config/tutorial'; + +# Let’s start with Python + +We're finally here! + +But first, let us tell you what Python is. Python is a very popular programming language that can be used +for creating websites, games, scientific software, graphics, and much, much more. + +Python originated in the late 1980s and its main goal is to be readable by human beings (not only machines!). +This is why it looks simpler than other programming languages, but don't worry – Python is also really powerful! + +# Python installation + +> **Note** If you already worked through the [installation steps](../installation/README.md), there's no need +to do this again – you can skip straight ahead to the next chapter! + +> For readers at home: this chapter is covered in the [Installing Python & Code Editor](https://www.youtube.com/watch?v=pVTaqzKZCdA) video. + +> This section is based on a tutorial by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots) + +Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want +you to install the latest version of Python 3, so if you have any earlier version, you will need to upgrade +it. If you already have version { PYTHON_MIN_VERSION } or higher you should be fine. + +Please install normal Python as follows, even when you have Anaconda installed on your computer. + +> **Note** Before you install Python on macOS, you should ensure your Mac settings allow installing packages + that aren't from the App Store. Go to System Preferences (it's in the Applications folder), click "Security + & Privacy," and then the "General" tab. If your "Allow apps downloaded from:" is set to "Mac App Store," + change it to "Mac App Store and identified developers." + +You need to go to the website https://www.python.org/downloads/ and download the latest Python installer: + +* Download Python { PYTHON_RELEASE }, +* Double click *python-{ PYTHON_RELEASE }-macos11.pkg* to run the installer. + +Verify the installation was successful by opening a command prompt and running the `python3` command: + +`command-line` +``` +$ python3 --version +Python 3.12.3 +``` +The version shown may be different from { PYTHON_RELEASE } -- it should match the version you installed. + +---- + +If you have any doubts, or if something went wrong and you have no idea what to do next, please ask your coach! Sometimes things don't go smoothly and it's better to ask for help from someone with more experience. diff --git a/src/pages/en/macos/05_python_introduction.mdx b/src/pages/en/macos/05_python_introduction.mdx new file mode 100644 index 0000000..4d60b1d --- /dev/null +++ b/src/pages/en/macos/05_python_introduction.mdx @@ -0,0 +1,1072 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Python Introduction +currentOS: macos +order: 5 +--- +import ErrorIcon from "../../../components/ErrorIcon"; + +# Introduction to Python + +> Part of this chapter is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots). + +Let's write some code! + +## Python prompt + +> For readers at home: this part is covered in the [Python Basics: Integers, Strings, Lists, Variables and Errors](https://www.youtube.com/watch?v=MO63L4s-20U) video. + +To start playing with Python, we need to open up a *command line* on your computer. You should already know how to do that – you learned it in the [Intro to Command Line](../intro_to_command_line/README.md) chapter. + +Once you're ready, follow the instructions below. + +We want to open up a Python console, so type in `python` on Windows or `python3` on Mac OS/Linux and hit `enter`. + +`command-line` +``` +$ python3 +Python 3.12.3 (...) +Type "help", "copyright", "credits" or "license" for more information. +>>> +``` + +## Your first Python command! + +After running the Python command, the prompt changed to `>>>`. For us this means that for now we may only +use commands in the Python language. You don't have to type in `>>>` – Python will do that for you. + +If you want to exit the Python console at any point, type `exit()` or use the shortcut `Ctrl + Z` for +Windows and `Ctrl + D` for Mac/Linux. Then you won't see `>>>` any longer. + +For now, we don't want to exit the Python console. We want to learn more about it. Let's start by typing +some math, like `2 + 3` and hitting `enter`. + +`command-line` +```python +>>> 2 + 3 +5 +``` + +Nice! See how the answer popped out? Python knows math! You could try other commands like: + +- `4 * 5` +- `5 - 1` +- `40 / 2` + +To perform exponential calculation, say 2 to the power 3, we type: +`command-line` +```python +>>> 2 ** 3 +8 +``` + +Have fun with this for a little while and then get back here. :) + +As you can see, Python is a great calculator. If you're wondering what else you can do… + +## Strings + +How about your name? Type your first name in quotes like this: + +`command-line` +```python +>>> "Ola" +'Ola' +``` + +You've now created your first string! It's a sequence of characters that can be processed by a computer. The +string must always begin and end with the same character. This may be single (`'`) or double (`"`) quotes +(there is no difference!) The quotes tell Python that what's inside of them is a string. + +Strings can be strung together. Try this: + +`command-line` +```python +>>> "Hi there " + "Ola" +'Hi there Ola' +``` + +You can also multiply strings with a number: + +`command-line` +```python +>>> "Ola" * 3 +'OlaOlaOla' +``` + +If you need to put an apostrophe inside your string, you have two ways to do it. + +Using double quotes: + +`command-line` +```python +>>> "Runnin' down the hill" +"Runnin' down the hill" +``` + +or escaping the apostrophe with a backslash (`\`): + +`command-line` +```python +>>> 'Runnin\' down the hill' +"Runnin' down the hill" +``` + +Nice, huh? To see your name in uppercase letters, type: + +`command-line` +```python +>>> "Ola".upper() +'OLA' +``` + +You just used the `upper` __method__ on your string! A method (like `upper()`) is a sequence of instructions +that Python has to perform on a given object (`"Ola"`) once you call it. + +If you want to know the number of letters contained in your name, there is a __function__ for that too! + +`command-line` +```python +>>> len("Ola") +3 +``` + +Wonder why sometimes you call functions with a `.` at the end of a string (like `"Ola".upper()`) and sometimes +you first call a function and place the string in parentheses? Well, in some cases, functions belong to +objects, like `upper()`, which can only be performed on strings. In this case, we call the function a +__method__. Other times, functions don't belong to anything specific and can be used on different types of +objects, just like `len()`. That's why we're giving `"Ola"` as a parameter to the `len` function. + +### Summary + +OK, enough of strings. So far you've learned about: + +- __the prompt__ – typing commands (code) into the Python prompt results in answers in Python +- __numbers and strings__ – in Python numbers are used for math and strings for text objects +- __operators__ – like `+` and `*`, combine values to produce a new one +- __functions__ – like `upper()` and `len()`, perform actions on objects. + +These are the basics of every programming language you learn. Ready for something harder? We bet you are! + +## Errors + +Let's try something new. Can we get the length of a number the same way we could find out the length of our +name? Type in `len(304023)` and hit `enter`: + + `command-line` +```python +>>> len(304023) +Traceback (most recent call last): + File "", line 1, in +TypeError: object of type 'int' has no len() +``` + +We got our first error! The icon is our way of giving you a heads up that the code you +are about to run won't work as expected. Making mistakes (even intentional ones) are an important part of learning! + + It says that objects of type "int" (integers, whole numbers) have no length. So what can we do now? Maybe + we can write our number as a string? Strings have a length, right? + +`command-line` +```python +>>> len(str(304023)) +6 +``` + +It worked! We used the `str` function inside of the `len` function. `str()` converts everything to strings. + +- The `str` function converts things into __strings__ +- The `int` function converts things into __integers__ + +> Important: we can convert numbers into text, but we can't necessarily convert text into numbers – what +would `int('hello')` be anyway? + +## Variables + +An important concept in programming is variables. A variable is nothing more than a name for something so +you can use it later. Programmers use these variables to store data, make their code more readable and so +they don't have to keep remembering what things are. + +Let's say we want to create a new variable called `name`: + +`command-line` +```python +>>> name = "Ola" +``` + +We type name equals Ola. + +As you've noticed, your program didn't return anything like it did before. So how do we know that the +variable actually exists? Enter `name` and hit `enter`: + +`command-line` +```python +>>> name +'Ola' +``` + +Yippee! Your first variable! :) You can always change what it refers to: + +`command-line` +```python +>>> name = "Sonja" +>>> name +'Sonja' +``` + +You can use it in functions too: + +`command-line` +```python +>>> len(name) +5 +``` + +Awesome, right? Now, variables can be anything – numbers too! Try this: + +`command-line` +```python +>>> a = 4 +>>> b = 6 +>>> a * b +24 +``` + +But what if we used the wrong name? Can you guess what would happen? Let's try! + + `command-line` +```python +>>> city = "Tokyo" +>>> ctiy +Traceback (most recent call last): + File "", line 1, in +NameError: name 'ctiy' is not defined +``` + +An error! As you can see, Python has different types of errors and this one is called a **NameError**. +Python will give you this error if you try to use a variable that hasn't been defined yet. If you encounter +this error later, check your code to see if you've mistyped any names. + +Play with this for a while and see what you can do! + + +## The print function + +Try this: + +`command-line` +```python +>>> name = 'Maria' +>>> name +'Maria' +>>> print(name) +Maria +``` + +When you just type `name`, the Python interpreter responds with the string *representation* of the variable +'name', which is the letters M-a-r-i-a, surrounded by single quotes, ''. When you say `print(name)`, Python +will "print" the contents of the variable to the screen, without the quotes, which is neater. + +As we'll see later, `print()` is also useful when we want to print things from inside functions, or when we +want to print things on multiple lines. + + +## Lists + +Beside strings and integers, Python has all sorts of different types of objects. Now we're going to introduce +one called __list__. Lists are exactly what you think they are: objects which are lists of other objects. :) + +Go ahead and create a list: + +`command-line` +```python +>>> [] +[] +``` + +Yes, this list is empty. Not very useful, right? Let's create a list of lottery numbers. We don't want to +repeat ourselves all the time, so we will put it in a variable, too: + +`command-line` +```python +>>> lottery = [3, 42, 12, 19, 30, 59] +``` + +All right, we have a list! What can we do with it? Let's see how many lottery numbers there are in a list. +Do you have any idea which function you should use for that? You know this already! + +`command-line` +```python +>>> len(lottery) +6 +``` + +Yes! `len()` can give you a number of objects in a list. Handy, right? Maybe we will sort it now: + +`command-line` +```python +>>> lottery.sort() +``` + +This doesn't return anything, it just changed the order in which the numbers appear in the list. Let's print it out again and see what happened: + +`command-line` +```python +>>> print(lottery) +[3, 12, 19, 30, 42, 59] +``` + +As you can see, the numbers in your list are now sorted from the lowest to highest value. Congrats! + +Maybe we want to reverse that order? Let's do that! + +`command-line` +```python +>>> lottery.reverse() +>>> print(lottery) +[59, 42, 30, 19, 12, 3] +``` + +If you want to add something to your list, you can do this by typing this command: + +`command-line` +```python +>>> lottery.append(199) +>>> print(lottery) +[59, 42, 30, 19, 12, 3, 199] +``` + +If you want to show only the first number, you can do this by using __indexes__. An index is the number that +says where in a list an item occurs. Programmers prefer to start counting at 0, so the first object in your +list is at index 0, the next one is at 1, and so on. Try this: + +`command-line` +```python +>>> print(lottery[0]) +59 +>>> print(lottery[1]) +42 +``` + +As you can see, you can access different objects in your list by using the list's name and the object's +index inside of square brackets. + +To delete something from your list you will need to use __indexes__ as we learned above and the `pop()` +method. Let's try an example and reinforce what we learned previously; we will be deleting the first number +of our list. + +`command-line` +```python +>>> print(lottery) +[59, 42, 30, 19, 12, 3, 199] +>>> print(lottery[0]) +59 +>>> lottery.pop(0) +59 +>>> print(lottery) +[42, 30, 19, 12, 3, 199] +``` + +That worked like a charm! + +For extra fun, try some other indexes: 6, 7, 1000, -1, -6 or -1000. See if you can predict the result before +trying the command. Do the results make sense? + +You can find a list of all available list methods in this chapter of the Python documentation: +https://docs.python.org/3/tutorial/datastructures.html + +## Dictionaries + +> For readers at home: this part is covered in the [Python Basics: Dictionaries](https://www.youtube.com/watch?v=ZX1CVvZLE6c) video. + +A dictionary is similar to a list, but you access values by looking up a key instead of a numeric index. A +key can be any string or number. The syntax to define an empty dictionary is: + +`command-line` +```python +>>> {} +{} +``` + +This shows that you just created an empty dictionary. Hurray! + +Now, try writing the following command (try substituting your own information, too): + +`command-line` +```python +>>> participant = {'name': 'Ola', 'country': 'Poland', 'favorite_numbers': [7, 42, 92]} +``` + +With this command, you just created a variable named `participant` with three key–value pairs: + +- The key `name` points to the value `'Ola'` (a `string` object), +- `country` points to `'Poland'` (another `string`), +- and `favorite_numbers` points to `[7, 42, 92]` (a `list` with three numbers in it). + +You can check the content of individual keys with this syntax: + +`command-line` +```python +>>> print(participant['name']) +Ola +``` + +See, it's similar to a list. But you don't need to remember the index – just the name. + +What happens if we ask Python the value of a key that doesn't exist? Can you guess? Let's try it and see! + + `command-line` +```python +>>> participant['age'] +Traceback (most recent call last): + File "", line 1, in +KeyError: 'age' +``` + +Look, another error! This one is a **KeyError**. Python is helpful and tells you that the key `'age'` +doesn't exist in this dictionary. + +When should you use a dictionary or a list? Well, that's a good point to ponder. Think about the answer +before looking at it in the next line. + +- Do you just need an ordered sequence of items? Go for a list. +- Do you need to associate values with keys, so you can look them up efficiently (by key) later on? Use a +dictionary. + +Like lists, using the `len()` function on the dictionaries returns the number of key–value pairs in the +dictionary. Go ahead and type in this command: + +`command-line` +```python +>>> len(participant) +3 +``` + +Dictionaries, like lists, are *mutable*, meaning that they can be changed after they are created. You can +add new key–value pairs to a dictionary after it is created, like this: + +`command-line` +```python +>>> participant['favorite_language'] = 'Python' +``` + +I hope it makes sense up to now. :) Ready for some more fun with dictionaries? Read on for some amazing things. + +You can use the `pop()` method to delete an item in the dictionary. Say, if you want to delete the entry +corresponding to the key `'favorite_numbers'`, type in the following command: + +`command-line` +```python +>>> participant.pop('favorite_numbers') +[7, 42, 92] +>>> participant +{'country': 'Poland', 'favorite_language': 'Python', 'name': 'Ola'} +``` + +As you can see from the output, the key–value pair corresponding to the 'favorite_numbers' key has been deleted. + +As well as this, you can also change a value associated with an already-created key in the dictionary. Type this: + +`command-line` +```python +>>> participant['country'] = 'Germany' +>>> participant +{'country': 'Germany', 'favorite_language': 'Python', 'name': 'Ola'} +``` + +As you can see, the value of the key `'country'` has been altered from `'Poland'` to `'Germany'`. :) +Exciting? Hurrah! You just learned another amazing thing. + +### Summary + +Awesome! You know a lot about programming now. In this last part you learned about: + +- __errors__ – you now know how to read and understand errors that show up if Python doesn't understand a +command you've given it +- __variables__ – names for objects that allow you to code more easily and to make your code more readable +- __lists__ – lists of objects stored in a particular order +- __dictionaries__ – objects stored as key–value pairs + +Excited for the next part? :) + +## Compare things + +> For readers at home: this part is covered in the [Python Basics: Comparisons](https://www.youtube.com/watch?v=7bzxqIKYgf4) video. + +A big part of programming involves comparing things. What's the easiest thing to compare? Numbers! Let's +see how that works: + +`command-line` +```python +>>> 5 > 2 +True +>>> 3 < 1 +False +>>> 5 > 2 * 2 +True +>>> 1 == 1 +True +>>> 5 != 2 +True +>>> len([1, 2, 3]) > len([4, 5]) +True +``` + +We gave Python some numbers to compare. As you can see, not only can Python compare numbers, but it can also +compare values of mathematical expressions like `2 * 2` and function results like the `2` returned by +`len([4, 5])`. Nice, huh? + +Do you wonder why we put two equal signs `==` next to each other to compare if numbers are equal? We use a +single `=` for assigning values to variables. You always, __always__ need to put two of them – `==` – if +you want to check if things are equal to each other. We can also state that things are unequal to each other. +For that, we use the symbol `!=`, as shown in the example above. + +Give Python two more tasks: + +`command-line` +```python +>>> 6 >= 12 / 2 +True +>>> 3 <= 2 +False +``` + +We've seen `>` and `<`, but what do `>=` and `<=` mean? Read them like this: + +- x `>` y means: x is greater than y +- x `<` y means: x is less than y +- x `<=` y means: x is less than or equal to y +- x `>=` y means: x is greater than or equal to y + +Awesome! Wanna do one more? Try this: + +`command-line` +```python +>>> 6 > 2 and 2 < 3 +True +>>> 3 > 2 and 2 < 1 +False +>>> 3 > 2 or 2 < 1 +True +``` + +You can give Python as many numbers to compare as you want, and it will give you an answer! Pretty smart, right? + +- __and__ – if you use the `and` operator, both comparisons have to be True in order for the whole command to be True +- __or__ – if you use the `or` operator, only one of the comparisons has to be True in order for the whole +command to be True + +Have you heard of the expression "comparing apples to oranges"? Let's try the Python equivalent: + + `command-line` +```python +>>> 1 > 'django' +Traceback (most recent call last): + File "", line 1, in +TypeError: '>' not supported between instances of 'int' and 'str' +``` + +Here you see that just like in the expression, Python is not able to compare a number (`int`) and a string (`str`). +Instead, it shows a **TypeError** and tells us the two types can't be compared together. + +## Boolean + +Incidentally, you just learned about a new type of object in Python. It's called __Boolean__. + +There are only two Boolean objects: +- True +- False + +But for Python to understand this, you need to always write it as 'True' (first letter uppercase, with the +rest of the letters lowercased). __true, TRUE, and tRUE won't work – only True is correct.__ (The same applies to 'False' as well.) + +Booleans can be variables, too! See here: + +`command-line` +```python +>>> a = True +>>> a +True +``` + +You can also do it this way: + +`command-line` +```python +>>> a = 2 > 5 +>>> a +False +``` + +Practice and have fun with Booleans by trying to run the following commands: + +- `True and True` +- `False and True` +- `True or 1 == 1` +- `1 != 2` + +Congrats! Booleans are one of the coolest features in programming, and you just learned how to use them! + +# Save it! + +> For readers at home: this part is covered in the [Python Basics: Saving files and "If" statement](https://www.youtube.com/watch?v=dOAg6QVAxyk) video. + + +So far we've been writing all our python code in the interpreter, which limits us to entering one line of +code at a time. Normal programs are saved in files and executed by our programming language __interpreter__ +or __compiler__. So far we've been running our programs one line at a time in the Python __interpreter__. +We're going to need more than one line of code for the next few tasks, so we'll quickly need to: + +- Exit the Python interpreter +- Open up our code editor of choice +- Save some code into a new python file +- Run it! + +To exit from the Python interpreter that we've been using, type the `exit()` function + +`command-line` +```python +>>> exit() +$ +``` + +This will put you back into the command prompt. + +Earlier, we picked out a code editor from the [code editor](/en/code_editor/) section. We'll need to open +the editor now and write some code into a new file: + +`editor` +```python +print('Hello, Django girls!') +``` + +Obviously, you're a pretty seasoned Python developer now, so feel free to write some code that you've learned today. + +Now we need to save the file and give it a descriptive name. Let's call the file **python_intro.py** and +save it to your desktop. We can name the file anything we want, but the important part here is to make sure +the file ends in __.py__. The __.py__ extension tells our operating system that this is a +**Python executable file** and Python can run it. + +> **Note** You should notice one of the coolest thing about code editors: colors! In the Python console, +everything was the same color; now you should see that the `print` function is a different color from the +string. This is called "syntax highlighting", and it's a really useful feature when coding. The color of +things will give you hints, such as unclosed strings or a typo in a keyword name (like the `def` in a +function, which we'll see below). This is one of the reasons we use a code editor. :) + + +With the file saved, it's time to run it! Using the skills you've learned in the command line section, use +the terminal to **change directories** to the desktop. + +On a Mac, the command will look something like this: + +`command-line` +``` +$ cd ~/Desktop +``` + +If you get stuck, ask for help. That's exactly what the coaches are here for! + +Now use Python to execute the code in the file like this: + +`command-line` +``` +$ python3 python_intro.py +Hello, Django girls! +``` + +Alright! You just ran your first Python program that was saved to a file. Feel awesome? + +You can now move on to an essential tool in programming: + +## If … elif … else + +Lots of things in code should be executed only when given conditions are met. That's why Python has +something called __if statements__. + +Replace the code in your **python_intro.py** file with this: + +`python_intro.py` +```python +if 3 > 2: +``` + +If we were to save and run this, we'd see an error like this: + + `command-line` +``` +$ python3 python_intro.py +File "python_intro.py", line 2 + ^ +SyntaxError: unexpected EOF while parsing +``` + +Python expects us to give further instructions to it which are 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: + +`python_intro.py` +```python +if 3 > 2: + print('It works!') +``` + +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 result is 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 as long as your text editor is set to do so. When you +made your choice, don't change it! If you already indented with 4 spaces, make any future indentation with +4 spaces, too - otherwise you may run into problems. + +Save it and give it another run: + +`command-line` +```python +$ python3 python_intro.py +It works! +``` + +Note: Remember that on Windows, 'python3' is not recognized as a command. From now on, replace 'python3' +with 'python' to execute the file. + +### What if a condition isn't True? + +In previous examples, code was executed only when the conditions were True. But Python also has `elif` and +`else` statements: + +`python_intro.py` +```python +if 5 > 2: + print('5 is indeed greater than 2') +else: + print('5 is not greater than 2') +``` + +When this is run it will print out: + +`command-line` +``` +$ python3 python_intro.py +5 is indeed greater than 2 +``` + +If 2 were a greater number than 5, then the second command would be executed. Let's see how `elif` works: + +`python_intro.py` +```python +name = 'Sonja' +if name == 'Ola': + print('Hey Ola!') +elif name == 'Sonja': + print('Hey Sonja!') +else: + print('Hey anonymous!') +``` + +and executed: + +`command-line` +``` +$ python3 python_intro.py +Hey Sonja! +``` + +See what happened there? `elif` lets you add extra conditions that run if the previous conditions fail. + +You can add as many `elif` statements as you like after your initial `if` statement. For example: + +`python_intro.py` +```python +volume = 57 +if volume < 20: + print("It's kinda quiet.") +elif 20 <= volume < 40: + print("It's nice for background music") +elif 40 <= volume < 60: + print("Perfect, I can hear all the details") +elif 60 <= volume < 80: + print("Nice for parties") +elif 80 <= volume < 100: + print("A bit loud!") +else: + print("My ears are hurting! :(") +``` + +Python runs through each test in sequence and prints: + +`command-line` +``` +$ python3 python_intro.py +Perfect, I can hear all the details +``` + +## Comments + +Comments are lines beginning with `#`. You can write whatever you want after the `#` and Python will ignore +it. Comments can make your code easier for other people to understand. + +Let's see how that looks: + +`python_intro.py` +```python +# Change the volume if it's too loud or too quiet +if volume < 20 or volume > 80: + volume = 50 + print("That's better!") +``` + +You don't need to write a comment for every line of code, but they are useful for explaining why your code +is doing something, or providing a summary when it's doing something complex. + + +### Summary + +In the last few exercises you learned about: + +- __comparing things__ – in Python you can compare things by using `>`, `>=`, `==`, `<=`, `<` and the `and`, `or` operators +- __Boolean__ – a type of object that can only have one of two values: `True` or `False` +- __Saving files__ – storing code in files so you can execute larger programs. +- __if … elif … else__ – statements that allow you to execute code only when certain conditions are met. +- __comments__ - lines that Python won't run which let you document your code + +Time for the last part of this chapter! + +## Your own functions! + +> For readers at home: this part is covered in the [Python Basics: Functions](https://www.youtube.com/watch?v=5owr-6suOl0) video. + +Remember functions like `len()` that you can execute in Python? Well, good news – you will learn how to +write your own functions now! + +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 give it a go. Replace the code in +**python_intro.py** with the following: + +`python_intro.py` +```python +def hi(): + print('Hi there!') + print('How are you?') + +hi() +``` + +Okay, our first function is ready! + +You may wonder why we've written the name of the function at the bottom of the file. When we write +`def hi():` and the indented lines following, this is us writing instructions for what the `hi()` function +should do. Python will read and remember these instructions, but won't run the function yet. To tell Python +we want to run the function, we have to call the function with `hi()`. Python reads the file and executes +it from top to bottom, so we have to define the function in the file before we call it. + +Let's run this now and see what happens: + +`command-line` +``` +$ python3 python_intro.py +Hi there! +How are you? +``` + +Note: if it didn't work, don't panic! The output will help you to figure why: +- If you get a `NameError`, that probably means you typed something wrong, so you should check that you +used the same name when creating the function with `def hi():` and when calling it with `hi()`. +- If you get an `IndentationError`, check that both of the `print` lines have the same whitespace at the +start of a line: python wants all the code inside the function to be neatly aligned. +- If there's no output at all, check that the last `hi()` *isn't* indented - if it is, that line will +become part of the function too, and it will never get run. + +Let's build our first function with parameters. We will change the previous example – a function that says +'hi' to the person running it – with a name: + +`python_intro.py` +```python +def hi(name): +``` + +As you can see, we now gave our function a parameter that we called `name`: + +`python_intro.py` +```python +def hi(name): + if name == 'Ola': + print('Hi Ola!') + elif name == 'Sonja': + print('Hi Sonja!') + else: + print('Hi anonymous!') + +hi() +``` + +Remember: The `print` function is indented four spaces within the `if` statement. This is because the +function runs when the condition is met. Let's see how it works now: + + `command-line` +``` +$ python3 python_intro.py +Traceback (most recent call last): +File "python_intro.py", line 10, in + hi() +TypeError: hi() missing 1 required positional argument: 'name' +``` + +Oops, an error. Luckily, Python gives us a pretty useful error message. +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. Let's fix it at the bottom of the file: + +1python_intro.py` +```python +hi("Ola") +``` + +And run it again: + +`command-line` +``` +$ python3 python_intro.py +Hi Ola! +``` + +And if we change the name? + +`python_intro.py` +```python +hi("Sonja") +``` + +And run it: + +`command-line` +``` +$ python3 python_intro.py +Hi Sonja! +``` + +Now, what do you think will happen if you write another name in there? (Not Ola or Sonja.) Give it a try +and see if you're right. It should print out this: + +`command-line` +``` +Hi anonymous! +``` + +This is awesome, right? This way you don't have to repeat yourself every time you want to change the name +of the person the function is supposed to greet. And that's exactly why we need functions – you never want +to repeat your code! + +Let's do something smarter – there are more names than two, and writing a condition for each would be hard, +right? Replace the content of your file with the following: + +`python_intro.py` +```python +def hi(name): + print('Hi ' + name + '!') + +hi("Rachel") +``` + +Let's call the code now: + +`command-line` +``` +$ python3 python_intro.py +Hi Rachel! +``` + +Congratulations! You just learned how to write functions! :) + +## Loops + +> For readers at home: this part is covered in the [Python Basics: For Loop](https://www.youtube.com/watch?v=aEA6Rc86HF0) video. + +This is the last part already. That was quick, right? :) + +Programmers don't like to repeat themselves. Programming is all about automating things, so we don't want +to greet every person by their name manually, right? That's where loops come in handy. + +Still remember lists? Let's do a list of girls: + +`python_intro.py` +```python +girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You'] +``` + +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: + +`python_intro.py` +```python +for name in girls: +``` + +The `for` statement behaves similarly to the `if` statement; code below both of these need to be indented four spaces. + +Here is the full code that will be in the file: + +`python_intro.py` +```python +def hi(name): + print('Hi ' + name + '!') + +girls = ['Rachel', 'Monica', 'Phoebe', 'Ola', 'You'] +for name in girls: + hi(name) + print('Next girl') +``` + +And when we run it: + +`command-line` +``` +$ python3 python_intro.py +Hi Rachel! +Next girl +Hi Monica! +Next girl +Hi Phoebe! +Next girl +Hi Ola! +Next girl +Hi You! +Next girl +``` + +As you can see, everything you put inside a `for` statement with an indent will be repeated for every +element of the list `girls`. + +You can also use `for` on numbers using the `range` function: + +`python_intro.py` +```python +for i in range(1, 6): + print(i) +``` + +Which would print: + +`command-line` +``` +1 +2 +3 +4 +5 +``` + +`range` is a function that creates a list of numbers following one after the other (these numbers are +provided by you as parameters). + +Note that the second of these two numbers is not included in the list that is output by Python (meaning +`range(1, 6)` counts from 1 to 5, but does not include the number 6). That is because `range` is half-open, and by that we mean it includes the first value, but not the last. + +## Summary + +That's it. __You totally rock!__ This was a tricky chapter, so you should feel proud of yourself. We're +definitely proud of you for making it this far! + +For official and full python tutorial visit https://docs.python.org/3/tutorial/. This will give you a more +thorough and complete study of the language. Cheers! :) + +You might want to briefly do something else – stretch, walk around for a bit, rest your eyes – before going +on to the next chapter. :) + + +![Cupcake](/images/cupcake.png) diff --git a/src/pages/en/macos/07_django_installation.mdx b/src/pages/en/macos/07_django_installation.mdx new file mode 100644 index 0000000..c9314c2 --- /dev/null +++ b/src/pages/en/macos/07_django_installation.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django Installation +currentOS: macos +order: 7 +--- \ No newline at end of file diff --git a/src/pages/en/macos/08_django_start_project.mdx b/src/pages/en/macos/08_django_start_project.mdx new file mode 100644 index 0000000..5f8ba30 --- /dev/null +++ b/src/pages/en/macos/08_django_start_project.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Your first Django project! +currentOS: macos +order: 8 +--- \ No newline at end of file diff --git a/src/pages/en/macos/09_django_models.mdx b/src/pages/en/macos/09_django_models.mdx new file mode 100644 index 0000000..280dc54 --- /dev/null +++ b/src/pages/en/macos/09_django_models.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django Models +currentOS: macos +order: 9 +--- \ No newline at end of file diff --git a/src/pages/en/macos/10_django_admin.mdx b/src/pages/en/macos/10_django_admin.mdx new file mode 100644 index 0000000..7257161 --- /dev/null +++ b/src/pages/en/macos/10_django_admin.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django admin +currentOS: macos +order: 10 +--- \ No newline at end of file diff --git a/src/pages/en/macos/11_deploy.mdx b/src/pages/en/macos/11_deploy.mdx new file mode 100644 index 0000000..793f8de --- /dev/null +++ b/src/pages/en/macos/11_deploy.mdx @@ -0,0 +1,6 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Deploy! +currentOS: macos +order: 11 +--- \ No newline at end of file diff --git a/src/pages/en/macos/index.md b/src/pages/en/macos/index.md deleted file mode 100644 index ca181ce..0000000 --- a/src/pages/en/macos/index.md +++ /dev/null @@ -1,18 +0,0 @@ ---- -layout: ../../../layouts/TutorialLayout.astro -title: Django Girls Tutorial (macOS) -currentOS: macos ---- - -# Welcome to the Django Girls Tutorial for macOS - -This version of the tutorial is designed for macOS users. -We’ll use Homebrew and other macOS-friendly tools to get Python and Django running smoothly. - -## What You’ll Learn -- Installing Python with Homebrew -- Setting up Django -- Creating your first Django project -- Building and deploying your app - -👉 Ready? Start with [Chapter 1](/macos/chapter1/). \ No newline at end of file diff --git a/src/pages/en/macos/index.mdx b/src/pages/en/macos/index.mdx new file mode 100644 index 0000000..8eacc9e --- /dev/null +++ b/src/pages/en/macos/index.mdx @@ -0,0 +1,228 @@ +--- +layout: ../../../layouts/TutorialLayout.astro +title: Django Girls Tutorial (macOS) +currentOS: macos +--- + +import { DJANGO_VERSION } from '../../../config/tutorial'; +import { PYTHON_VERSION } from '../../../config/tutorial'; +import { PYTHON_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_VERSION } from '../../../config/tutorial'; + +# Installation + +To install software on your machine, follow the instructions below: + +## Brief intro to the command line + +Many of the steps below reference the "console", "terminal", "command window", or "command line" -- +these all mean the same thing: a window on your computer where you can enter commands. When you get to the +main tutorial, you'll learn more about the command line. For now, the main thing you need to know is how to +open a command window and what it looks like: + +Go to Launchpad → Other → Terminal. + +### The Command-line Prompt + +Now you know how to open a command line, we just need to understand what the "prompt" is. + +If you're on Mac, you probably see a `$`, like this: + +`command-line` +``` +$ +``` + +Each command will be prepended by a `$` and one space, but you should not type it. Your computer will do it +for you. :) + +> Just a small note: in your case there may be something like `Olas-MacBook-Air:~ ola$` before the prompt +sign, and this is 100% OK. + +The part up to and including the `$` is called the *command line prompt*, or *prompt* for short. It prompts +you to input something there. + +In the tutorial, when we want you to type in a command, we will include the `$`, and occasionally more to +the left. Ignore the left part and only type in the command, which starts after the prompt. + +## Install Python +> For readers at home: this chapter is covered in the [Installing Python & Code Editor](https://www.youtube.com/watch?v=pVTaqzKZCdA) video. + +> This section is based on a tutorial by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots) + +Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want +you to install the latest version of Python 3, so if you have any earlier version, you will need to upgrade +it. If you already have version { PYTHON_MIN_VERSION } or higher you should be fine. + +Please install normal Python as follows, even when you have Anaconda installed on your computer. + +> **Note** Before you install Python on macOS, you should ensure your Mac settings allow installing packages +that aren't from the App Store. Go to System Preferences (it's in the Applications folder), click "Security +& Privacy," and then the "General" tab. If your "Allow apps downloaded from:" is set to "Mac App Store," +change it to "Mac App Store and identified developers." + +You need to go to the website https://www.python.org/downloads/ and download the latest Python installer: + +* Download Python { PYTHON_RELEASE }, +* Double click *python-{ PYTHON_RELEASE }-macos11.pkg* to run the installer. + +---- + +If you have any doubts, or if something went wrong and you have no idea what to do next, please ask your +coach! Sometimes things don't go smoothly and it's better to ask for help from someone with more experience. + +## Install a code editor +There are a lot of different editors and it largely boils down to personal preference. Most Python programmers +use complex but extremely powerful IDEs (Integrated Development Environments), such as PyCharm. As a beginner, +however, that's probably less suitable; our recommendations are equally powerful, but a lot simpler. + +Our suggestions are below, but feel free to ask your coach what their preferences are – it'll be easier to +get help from them. + +## Visual Studio Code + +Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and macOS. It includes +support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring. + +[Download it here](https://code.visualstudio.com/) + +## Gedit + +Gedit is an open-source, free editor, available for all operating systems. + +[Download it here](https://wiki.gnome.org/Apps/Gedit#Download) + +## Sublime Text + +Sublime Text is a very popular editor with a free evaluation period and it's available for all operating systems. + +[Download it here](https://www.sublimetext.com/) + +## Why are we installing a code editor? + +You might be wondering why we are installing this special code editor software, rather than using something +like Word or Notepad. + +The first reason is that code needs to be **plain text**, and the problem with programs like Word and +Textedit is that they don't actually produce plain text, they produce rich text (with fonts and formatting), +using custom formats like [RTF (Rich Text Format)](https://en.wikipedia.org/wiki/Rich_Text_Format). + +The second reason is that code editors are specialized for editing code, so they can provide helpful features +like highlighting code with color according to its meaning, or automatically closing quotes for you. + +We'll see all this in action later. Soon, you'll come to think of your trusty old code editor as one of your +favorite tools. :) + +## Set up virtualenv and install Django +> Part of this section is based on tutorials by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots). + +> Part of this section is based on the [django-marcador +tutorial](http://django-marcador.keimlink.de/) licensed under the Creative Commons +Attribution-ShareAlike 4.0 International License. The django-marcador tutorial +is copyrighted by Markus Zapke-Gründemann et al. + + +## Virtual environment + +Before we install Django we will get you to install an extremely useful tool to help keep your coding +environment tidy on your computer. It's possible to skip this step, but it's highly recommended to follow it +. Starting with the best possible setup will save you a lot of trouble in the future! + +So, let's create a **virtual environment** (also called a *virtualenv*). Virtualenv will isolate your +Python/Django setup on a per-project basis. This means that any changes you make to one website won't affect + any others you're also developing. Neat, right? + +All you need to do is find a directory in which you want to create the `virtualenv`; your home directory, +for example. On Windows, it might look like `C:\Users\Name\` (where `Name` is the name of your login). + +For this tutorial we will be using a new directory `djangogirls` from your home directory: + +`command-line` +``` +$ mkdir djangogirls +$ cd djangogirls +``` + +We will make a virtualenv called `myvenv`. The general command will be in the format: + +`command-line` +``` +$ python3 -m venv myvenv +``` + +We can create a `virtualenv` on macOS by running `python3 -m venv myvenv`. +It will look like this: + +`command-line` +``` +$ python3 -m venv myvenv +``` + +`myvenv` is the name of your `virtualenv`. You can use any other name, but stick to lowercase and use no +spaces. It is also a good idea to keep the name short as you'll be referencing it a lot! + +## Install Git +Git is a "version control system" used by a lot of programmers. This software can track changes to files +over time so that you can recall specific versions later. A bit like the "track changes" feature in word +processor programs (e.g., Microsoft Word or LibreOffice Writer), but much more powerful. + +## Installing Git +Download Git from [https://git-scm.com/download/mac](https://git-scm.com/download/mac) and follow the instructions. + +> **Note** If you are running OS X 10.6, 10.7, or 10.8, you will need to install the version of git from here: +[Git installer for OS X Snow Leopard](https://sourceforge.net/projects/git-osx-installer/files/git-2.3.5-intel-universal-snow-leopard.dmg/download) + +> It is recommended to install using brew (you may need to install homebrew if you don't already have it): +> ```brew install git``` +> +> Please note that some Mac M1/M2/M3 users may have the error `zsh: command not found: brew`. In that case, +please follow [this](https://stackoverflow.com/a/66019047) or [this](https://mac.install.guide/homebrew/zsh-command-not-found-brew) to fix the error. + +## Create a GitHub account +Go to [GitHub.com](https://www.github.com) and sign up for a new, free user account. Be sure to remember +your password (add it to your password manager, if you use one). + +## Create a PythonAnywhere account +PythonAnywhere is a service for running Python code on servers "in the cloud". We'll use +it for hosting our site, live and on the Internet. + +We will be hosting the blog we're building on PythonAnywhere. Sign up for a "Beginner" account on +PythonAnywhere (the free tier is fine, you don't need a credit card). + +* [www.pythonanywhere.com](https://www.pythonanywhere.com/) + +![The PythonAnywhere signup page showing button to create a free 'Beginner' account](/images/pythonanywhere_beginner_account_button.png) + +> **Note** When choosing your username here, bear in mind that your blog's URL will take the form +`yourusername.pythonanywhere.com`, so choose either your own nickname or a name for what your blog is all +about. Also, be sure to remember your password (add it to your password manager, if you use one). + + +## Creating a PythonAnywhere API token + +This is something you only need to do once. When you've signed up for PythonAnywhere, you'll be taken to +your dashboard. Find the link near the top right to your "Account" page: + +![Account link on the top right on the page](/images/pythonanywhere_account.png) + +then select the tab named "API token", and hit the button that says "Create new API token". + +![The API token tab on the Account page](/images/pythonanywhere_create_api_token.png) + + +# Start reading + +Congratulations, you are all set up and ready to go! If you still have some time before the workshop, it would be useful to start reading a few of the beginning chapters: + +* [How the internet works](/en/common/01-how-the-internet-works/) + +* [Introduction to the command line](/en/macos/02-intro_to_command_line/) + +* [Introduction to Python](/en/macos/03-python_introduction/) + +* [What is Django?](/en/common/06_django/) + + +# Enjoy the workshop! + +When you begin the workshop, you'll be able to go straight to [Your first Django project!](/en/macos/08_django_start_project/) because you already covered the material in the earlier chapters. diff --git a/src/pages/en/windows/03_python_installation.mdx b/src/pages/en/windows/03_python_installation.mdx index 92495d1..438eea0 100644 --- a/src/pages/en/windows/03_python_installation.mdx +++ b/src/pages/en/windows/03_python_installation.mdx @@ -7,6 +7,8 @@ order: 3 import { DJANGO_VERSION } from '../../../config/tutorial'; import { PYTHON_VERSION } from '../../../config/tutorial'; +import { PYTHON_RELEASE } from '../../../config/tutorial'; +import { PYTHON_MIN_VERSION } from '../../../config/tutorial'; # Let’s start with Python @@ -30,7 +32,7 @@ do this again – you can skip straight ahead to the next chapter! Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want you to install the latest version of Python 3, so if you have any earlier version, you will -need to upgrade it. If you already have version { PYTHON_VERSION } or higher you should be fine. +need to upgrade it. If you already have version { PYTHON_MIN_VERSION } or higher you should be fine. Please install normal Python as follows, even when you have Anaconda installed on your computer. diff --git a/src/pages/en/windows/05_python_introduction.mdx b/src/pages/en/windows/05_python_introduction.mdx index 148b841..703f4b2 100644 --- a/src/pages/en/windows/05_python_introduction.mdx +++ b/src/pages/en/windows/05_python_introduction.mdx @@ -640,8 +640,7 @@ $ This will put you back into the command prompt. 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 (or if you're using a Chromebook, create a new -file in the cloud IDE and open the file, which will be in the included code editor): +to open the editor now and write some code into a new file: `editor` ```python diff --git a/src/pages/en/windows/index.mdx b/src/pages/en/windows/index.mdx index 9160c73..282df3d 100644 --- a/src/pages/en/windows/index.mdx +++ b/src/pages/en/windows/index.mdx @@ -6,6 +6,7 @@ currentOS: windows import { DJANGO_VERSION } from '../../../config/tutorial'; import { PYTHON_VERSION } from '../../../config/tutorial'; +import { PYTHON_MIN_VERSION } from '../../../config/tutorial'; # Installation @@ -52,7 +53,9 @@ In the tutorial, when we want you to type in a command, we will include the `$` > This section is based on a tutorial by Geek Girls Carrots (https://github.com/ggcarrots/django-carrots) -Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want you to install the latest version of Python 3, so if you have any earlier version, you will need to upgrade it. If you already have version {PYTHON_VERSION} or higher you should be fine. +Django is written in Python. We need Python to do anything in Django. Let's start by installing it! We want +you to install the latest version of Python 3, so if you have any earlier version, you will need to upgrade +it. If you already have version {PYTHON_MIN_VERSION} or higher you should be fine. Please install normal Python as follows, even when you have Anaconda installed on your computer. @@ -64,13 +67,17 @@ First check whether your computer is running a 32-bit version or a 64-bit versio You can download Python for Windows from the website https://www.python.org/downloads/windows/. Click on the "Latest Python 3 Release - Python x.x.x" link. If your computer is running a **64-bit** version of Windows, download the **Windows x86-64 executable installer**. Otherwise, download the **Windows x86 executable installer**. After downloading the installer, you should run it (double-click on it) and follow the instructions there. -One thing to watch out for: During the installation, you will notice a window marked "Setup". Make sure you tick the "Add Python {PYTHON_VERSION} to PATH" or 'Add Python to your environment variables" checkbox and click on "Install Now", as shown here (it may look a bit different if you are installing a different version): +One thing to watch out for: During the installation, you will notice a window marked "Setup". Make sure you +tick the "Add Python {PYTHON_VERSION} to PATH" or 'Add Python to your environment variables" checkbox and +click on "Install Now", as shown here (it may look a bit different if you are installing a different version): ![Don't forget to add Python to the Path](/images/python-installation-options.png) -When the installation completes, you may see a dialog box with a link you can follow to learn more about Python or about the version you installed. Close or cancel that dialog -- you'll be learning more in this tutorial! +When the installation completes, you may see a dialog box with a link you can follow to learn more about Python +or about the version you installed. Close or cancel that dialog -- you'll be learning more in this tutorial! -Note: If you are using an older version of Windows (7, Vista, or any older version) and the Python {PYTHON_VERSION} installer fails with an error, then install all Windows Updates and try to install Python again. If you still have the error, try installing Python version {PYTHON_VERSION} from [Python.org](https://www.python.org/downloads/windows/). +Note: If you are using an older version of Windows (7, Vista, or any older version) and the Python +{PYTHON_VERSION} installer fails with an error, then install all Windows Updates and try to install Python again. If you still have the error, try installing Python version {PYTHON_VERSION} from [Python.org](https://www.python.org/downloads/windows/). > Django {DJANGO_VERSION} needs Python {PYTHON_VERSION} or greater, which does not support Windows XP or earlier versions. @@ -273,15 +280,15 @@ then select the tab named "API token", and hit the button that says "Create new Congratulations, you are all set up and ready to go! If you still have some time before the workshop, it would be useful to start reading a few of the beginning chapters: -* [How the internet works](/en/windows/01-how-the-internet-works.md) +* [How the internet works](/en/common/01-how-the-internet-works/) -* [Introduction to the command line](/en/windows/02-intro_to_command_line.md) +* [Introduction to the command line](/en/windows/02-intro_to_command_line/) -* [Introduction to Python](/en/windows/03-python_introduction.md) +* [Introduction to Python](/en/windows/03-python_introduction/) -* [What is Django?](/en/windows/04-django.md) +* [What is Django?](/en/common/06_django/) # Enjoy the workshop! -When you begin the workshop, you'll be able to go straight to [Your first Django project!](../django_start_project/README.md) because you already covered the material in the earlier chapters. +When you begin the workshop, you'll be able to go straight to [Your first Django project!](/en/windows/08_django_start_project/) because you already covered the material in the earlier chapters.