diff --git a/docs/index.md b/docs/index.md index 0ff7f76..ad8c630 100644 --- a/docs/index.md +++ b/docs/index.md @@ -52,9 +52,6 @@ A Discord community for people who write Python. {{ member_count }} of us so far [:octicons-arrow-right-24: Read the rules](rules/index.md) - - - - :material-bug:{ .lg .middle } __Found a mistake on the site?__ --- @@ -64,5 +61,4 @@ A Discord community for people who write Python. {{ member_count }} of us so far [:octicons-arrow-right-24: Contribute!](projects/our-projects/this-site.md) - diff --git a/docs/python-tutorial/chapters/01-python-history.md b/docs/python-tutorial/chapters/01-python-history.md new file mode 100644 index 0000000..2617892 --- /dev/null +++ b/docs/python-tutorial/chapters/01-python-history.md @@ -0,0 +1,36 @@ +--- +description: The very first chapter of the python tutorial. This covers a brief introduction to the history of Python. +--- + +# Brief Python History + +Before we get into learning how to use Python, it would be insightful to know about Python itself. +Why was it created and by whom? Where does Python stand today? + +The Python programming language was developed back in the late 1980s by Guido van Rossum in the +Netherlands. It was the successor of the ABC language which was used to teach students in +universities, but it lacked extensibility from being used in real-world programming tasks, which +brings us to Python. + +Van Rossum was formerly the Benevolent Dictator for Life (BDFL) of Python, until he stepped down as +the leader on July 12, 2018, after some controversies in the decision-making of adding the walrus +operator ([PEP 572](https://peps.python.org/pep-0572/)) to Python. + +The current governance of Python is held by a five-person committee elected by the core developers +called the Python Steering Council. The Python Steering Council is re-elected after every major +release (approximately 18 months). The Steering Council only controls the technical direction of the +Python language. + +As of August 2026, according to the TIOBE Community Index, Python is _the_ most popular programming +language in the world, used by millions of developers. Python is a versatile language used in many +domains such as AI/ML, cybersecurity, web development and data science to name a few. In this +tutorial, we will not be covering any of those domains, but instead we will start from the very +basics to build your fundamentals and prepare you to learn them on your own. + +
+ +![Image title](/static/python-tutorial/01-guido-portrait.jpg){ width="300" } + +
Guido van Rossum in 2014
+ +
diff --git a/docs/python-tutorial/chapters/02-intro-to-variables.md b/docs/python-tutorial/chapters/02-intro-to-variables.md new file mode 100644 index 0000000..9eb965d --- /dev/null +++ b/docs/python-tutorial/chapters/02-intro-to-variables.md @@ -0,0 +1,102 @@ +--- +description: Introduction to Python variables. +--- + +# Introduction to Variables + +??? note "Brief on Computer RAM" + + In this chapter, we will also be talking about Random Access Memory (RAM) to better understand + variables, but first, let's quickly cover what it is. RAM is where your computer stores the data of + a running program. It is fast, but volatile (wiped when power is lost, unlike disk storage). + Whenever you run your Python program, it loads it into the RAM and the instructions are then fed to + the CPU by your operating system. + +Variables give programmers an easy way to work with the computer's memory - they act as a bridge +that lets us use and manipulate memory without dealing with it directly. That is all variables do. +Different languages have different syntax, rules, types of variables, etc., but in the end, they are +all doing the same task. As far as we are concerned, Python does not have multiple types of +variables. + +Let's take a look at how we can define a variable in python: + +```python +name = "Alan" +``` + +Let's break this down. Over here, `name` is what we call a variable. `"Alan"` is the data we're +assigning to that variable through the assignment operator (`=`). + +!!! note + + We will learn more about data types and operators in later chapters. We will be focusing mostly on + variables for now. + +So, where does the memory that we discussed before come into play here? When we create a variable in +Python, it automatically reserves some space in memory and places the data there, which the variable +then points to. + +
+ +![Image title](/static/python-tutorial/02-memory.png){ width="600" } + +
+ The numbers indicate memory addresses and `~` indicate some random data from other programs, + irrelevant to us. You can imagine memory addresses in a similar concept to your house address. + They both are used to locate the whereabouts. +
+ +
+ +!!! note + + The above image is only an example of how variables point to memory addresses. Note that it is not a + complete representation of how computer memory looks like. Only meant for understanding purposes. + +In the diagram above, the variable `name` points to memory addresses `3` - `6`; but notice we never +had to specify that in our code. That's because Python handles this tracking for us automatically. +This is really the whole point of variables: instead of manually keeping track of which data lives +at which memory address, we can just use a variable name as a clear, readable label for that piece +of memory. + +Now that we understand why we use variables, we must also know the rules for declaring variables in +Python. There are 4 simple rules which follow: + +1. Variables cannot contain any special characters other than underscore (`_`). +1. Variables cannot have spaces in them, use underscores instead. +1. Variables cannot start with a number but can contain numbers inside or at the end of them. +1. Variables cannot share the same names as Python keywords (keywords are reserved words with + special functionality in Python). + +!!! note "Case Sensitivity" + + Python is a case sensitive language, meaning, in this piece of code: + + ```python + name = "Zeed" + Name = "Stun" + ``` + + There now exists two variables, `name` and `Name` instead of one. A good rule of thumb is to always + use `snake_casing` convention for your variables which is to keep your variables in all lower cases + and use underscores for word separation. + +## Examples + +Here is a list of **invalid** python variables: + +```python +full name = "James Bond" # Contains a space +age! = 90 # Contains a special character +finally = "yes" # Conflicts with the Python keyword "finally" +20money = 900 # Starts with a number +``` + +Below is a list of **valid** python variables: + +```python +full_name = "Bonding Jameful" +age = 90 +answer_1 = "yes" +answer_2 = "no" +``` diff --git a/docs/python-tutorial/chapters/03-data-types-pt01.md b/docs/python-tutorial/chapters/03-data-types-pt01.md new file mode 100644 index 0000000..1200d23 --- /dev/null +++ b/docs/python-tutorial/chapters/03-data-types-pt01.md @@ -0,0 +1,82 @@ +--- +description: Part 1 of Python Data Types +--- + +# Data Types Part 01 + +From our previous chapter, we learned about variables with a small primer on data types. In this +chapter we will understand what they are and how we use them. + +## What are data types? + +In Python, we can define a data type to be a concrete representation of any value. A value could be +someone's name, money, age, or even a game boss's HP. + +## String Data Type + +Let's take the example from our previous chapter: + +```python +name = "Alan" +``` + +From here, `"Alan"` is what we call the string data type. It is used to represent textual data +within its quotes (`"`). Python strings can use both double and single quotes, but the opening and +closing quote must match. + +Examples of **valid** strings: + +```python +country = "New Zealand" +capital = 'Wellington' +``` + +Examples of **invalid** strings: + +```python +language = 'English" +favourite_colour = "Purple' +``` + +## Integer Data Type + +Just like in mathematics, we have an integer data type in Python. An integer is a number that can be +written without a fractional or decimal component. + +Example: + +```python +age = 34 +player_hp = 90 +player_hp_max = 100 +``` + +## Floating Point Data Type + +Floating Point data type, more commonly called Float, is a data type representing decimal numbers. + +Example: + +```python +wallet_money_amount = 124.43 +bank_money_amount = 33.2 +``` + +## Boolean Data Type + +Boolean (or bool) is a special data type in Python. A boolean can only be one of two values: `True` +or `False`. + +Example: + +```python +is_running = True +is_flying = False +``` + +!!! warning + + `True` and `False` are case sensitive. `true` is **not** the same as `True`. + +Booleans are more commonly used in conditional statements, loops and comparison operators which we +will learn in later chapters as a continuation. diff --git a/docs/python-tutorial/chapters/04-built-in-functions-pt01.md b/docs/python-tutorial/chapters/04-built-in-functions-pt01.md new file mode 100644 index 0000000..975baa4 --- /dev/null +++ b/docs/python-tutorial/chapters/04-built-in-functions-pt01.md @@ -0,0 +1,205 @@ +--- +description: Part 1 of Built-in Functions +--- + +# Built-in Functions Part 01 + +## What are built-in functions? + +A function is a reusable block of code that performs a specific task. You give it some input +(sometimes), it does something with it, and it gives you back a result. Python has many built-in +functions which you can use to make your life easier. In this section, we will take a look at +`print`, `input` and some type-casting functions in detail for this chapter. + +## `print()` function + +The `print` function is used to show text on the screen (also known as the terminal or standard +output). With this, you can write your first program that every programmer writes when they learn a +new language: + +```python +print("Hello World!") +``` + +When you run this program, it should show `Hello World!` in your terminal. We are not limited to +only strings, we can even print out other kinds of data types! + +```python +print("My name is Alex") +print(56) +print(3.14) +print(False) +``` + +We can also print variables, this prints out the data present within them. + +```python +age = 43 +print(age) + +language = "python" +print(language) +``` + +The output of the code above will be: + +```text +43 +python +``` + +Each `print` statement prints on a new line. You can print multiple things using a single `print` +statement by separating them using commas (`,`), which will result in all the values being printed +on the same line separated by spaces. + +```python +print("Value of pi is", 3.14) +``` + +This allows us to also print out variables in the same line too. + +```python +name = "Radagon" +money = 30 +print(name, "has", money, "dollars to his name.") +``` + +This will print out `Radagon has 30 dollars to his name.` + +!!! note + + Keep in mind that the comma separation of values only work in the `print` function. + +## `input()` function + +The `input` function is used to take input from the user through the terminal. It returns everything +the user types until the user hits the ++enter++ key. You can also pass in a string to the function +which will appear as a prompt before asking for input, like so: + +```python +input("Enter your name: ") +``` + +The above code will print `Enter your name: ` followed by a blinking cursor where you can type the +actual input. Now, to actually use the value entered by the user, you need to assign the value +returned by the `input` function to a variable. + +```python +name = input("Enter your name: ") +print("Hello", name, "! Hope you have a wonderful day.") +``` + +The output of this will be: + +```text +Enter your name: Krish +Hello Krish ! Hope you have a wonderful day. +``` + +!!! note + + You must keep in mind that the `input` function will _always_ return the user input as a string. + +## Type Casting + +Type casting is the process of converting one data type to another. Remember the note from above? +> You must keep in mind that the `input` function will _always_ return the user input as a string. + +There will be scenarios where you need the input function to return a data type other than a string. +One such place could be when you want to make a calculator that adds two numbers. Let's take a look +at how we would do it without any type casting. + +```python +num1 = input("Enter the first number: ") +num2 = input("Enter the second number: ") +result = num1 + num2 + +print("The sum of", num1, "and", num2, "is", result) +``` + +If we run this program and enter two numbers, this is the result we would get: + +```text +Enter the first number: 23 +Enter the second number: 12 +The sum of 23 and 12 is 2312 +``` + +As we can see, `23 + 12` is clearly _not_ supposed to be 2312. So what's happening here? + +The variables `num1` and `num2` hold a string data type of the user input. When you perform an +addition operation on two strings (`result = num1 + num2`) you are simply joining two strings +together, side-by-side. This is also called string concatenation which we will learn more about in +the next chapter. + +This is where type casting comes to fix this problem. We need to convert the string data type to +integer so the computer can follow regular arithmetic for our use case. + +```python +num1 = input("Enter the first number: ") +num2 = input("Enter the second number: ") + +int_num1 = int(num1) +int_num2 = int(num2) + +result = int_num1 + int_num2 + +print("The sum of", num1, "and", num2, "is", result) +``` + +Running it now gives the correct result: + +```text +Enter the first number: 23 +Enter the second number: 12 +The sum of 23 and 12 is 35 +``` + +`int_num1 = int(num1)` and `int_num2 = int(num2)` is what we call type casting. We're casting the +input string to an integer, then assigning it to a variable. (Assigning the cast value to a separate +variable is optional and can be omitted.) + +Another common way to write the code above is: + +```python +num1 = int(input("Enter the first number: ")) +num2 = int(input("Enter the second number: ")) + +result = num1 + num2 + +print("The sum of", num1, "and", num2, "is", result) +``` + +Both the programs perform the same way, with the only difference being that we aren't creating an +extra variable in the latter. Whenever you see functions being executed within another function: + +```python +num1 = int(input("Enter the first number: ")) +``` + +Remember that the innermost function is the one that always executes first. In this case, the +`input` function is the one that fires first. It waits for the user to give an input. After an input +is given, the data transfers to the `int()` function - converting the string data to an integer (if +the input is valid). Finally, the data gets assigned to the variables. + +The example above shows you how to convert user input to an integer. In case you want to use +decimals instead of integers, you can type cast it to `float`. + +```python +num1 = float(input("Enter the first number: ")) +num2 = float(input("Enter the second number: ")) + +result = num1 + num2 + +print("The sum of", num1, "and", num2, "is", result) +``` + +Similarly, we can cast data to other types, such as strings and booleans, by calling their +respective functions. + +| Data Type | Function Call | +| ---------- | ------------- | +| String | `str()` | +| Integers | `int()` | +| Floats | `float()` | +| Boolean | `bool()` | diff --git a/docs/python-tutorial/chapters/index.md b/docs/python-tutorial/chapters/index.md new file mode 100644 index 0000000..7a6f00c --- /dev/null +++ b/docs/python-tutorial/chapters/index.md @@ -0,0 +1,57 @@ +--- +description: List of topics to be covered in the Python tutorial. +--- + +# Topics + +The topics in this tutorial are organized into 3 levels, with each level covering progressively more +in-depth Python features. + +## Level 1 + +- [Brief Python History](01-python-history.md) +- [Introduction to Variables](02-intro-to-variables.md) +- [Data Types pt 1 (`string`, `int`, `float` & `bool`)](03-data-types-pt01.md) +- [Builtin Functions pt 1 (`print` & `input` and type casting)](04-built-in-functions-pt01.md) +- String Interpolation & Concatenation (f-strings, str.format & +) +- Operators pt 1 (Comparison & Arithmetic) +- Conditional Statements (`if`, `else` & `elif`) +- Operators & Conditional Statements pt 2 (Logical operators) +- Data Types pt 2 (`list` & `tuple`) +- Data Types pt 3 (dictionary) +- Operators pt 3 (membership operators) +- Loops pt 1 (`for` loops) +- Loops pt 2 (`while` loops) +- Loops pt 3 (`else`, `break` & `continue`) + +## Level 2 + +- Builtin Functions pt 2 (`sum`, `min`, `max` & `round`) +- Error Handling pt 1 (`try`, `except`, `ValueError`) +- Error Handling pt 2 (`finally` & `else`) +- User Defined Functions pt 1 (`def`, parameters & arguments) +- User Defined Functions pt 2 (types of parameters) +- User Defined Functions pt 3 (`return` & `print` vs `return`) +- User Defined Functions pt 4 (Scopes) +- Standard Library pt 1 (random) +- Standard Library pt 2 (time) +- File Handling pt 1 (opening file [manual & context manager], reading & writing to file) +- File Handling pt 2 (JSON file handling) +- Data Types pt 4 (list & dictionary comprehension and generators) + +## Level 3 + +- PIP Package Manager pt 1 (pip install & pip uninstall) +- Third party libraries (requests) +- PIP Package Manager pt 2 (pip freeze & requirements.txt) +- Virtual Environment +- Classes pt 1 (defining basic class & class attributes) +- Classes pt 2 (the `__init__` dunder method & instance attributes) +- Classes pt 3 (difference in class & instance variables) +- Classes pt 4 (user defined methods) +- Classes pt 5 (subclassing & super) +- Error Handling pt 3 (custom errors & raising it) +- User Defined Functions pt 5 (decorators) +- Classes pt 6 (`@staticmethod` & `@classmethod`) +- Classes pt 7 (`@property` getters & setters) +- Classes pt 8 (enums & dataclasses) diff --git a/docs/python-tutorial/index.md b/docs/python-tutorial/index.md new file mode 100644 index 0000000..054882c --- /dev/null +++ b/docs/python-tutorial/index.md @@ -0,0 +1,20 @@ +--- +description: Hand crafted Python tutorial from the wonderful community. +--- + +# Python Tutorial + +## What is this? + +This python tutorial section will attempt to teach you the core concepts of python in a +beginner-friendly manner. The way this tutorial is laid out allows you to read it in a contiguous +manner, connecting all the chapters in a smooth flow. You can look at the next page for the list of +topics to be covered. + +## What is this not? + +This is _not_ your typical "beginner to pro" tutorial scam and neither will we advertise it as so. +Any course, book or YouTube video that says so otherwise is probably a scam and can waste your time. +Building expertise comes from making projects yourself and learning every nitty-gritty things +yourself. You will not be able to achieve this just by reading this tutorial alone either, but we +will provide you a stepping stone to get started with this. diff --git a/docs/static/python-tutorial/01-guido-portrait.jpg b/docs/static/python-tutorial/01-guido-portrait.jpg new file mode 100644 index 0000000..12dcbf4 Binary files /dev/null and b/docs/static/python-tutorial/01-guido-portrait.jpg differ diff --git a/docs/static/python-tutorial/02-memory.png b/docs/static/python-tutorial/02-memory.png new file mode 100644 index 0000000..bbd145b Binary files /dev/null and b/docs/static/python-tutorial/02-memory.png differ diff --git a/zensical.toml b/zensical.toml index 414f94e..21a25e3 100644 --- a/zensical.toml +++ b/zensical.toml @@ -18,6 +18,14 @@ nav = [ { "Home" = "index.md" }, { "Getting started" = "getting-started/index.md" }, + { "Python Tutorial" = [ + "python-tutorial/index.md", + "python-tutorial/chapters/index.md", + "python-tutorial/chapters/01-python-history.md", + "python-tutorial/chapters/02-intro-to-variables.md", + "python-tutorial/chapters/03-data-types-pt01.md", + "python-tutorial/chapters/04-built-in-functions-pt01.md", + ] }, { "Resources" = [ "resources/index.md", "resources/getting-started.md",