├── .github ├── ISSUE_TEMPLATE │ ├── bug_report.md │ ├── custom.md │ └── feature_request.md └── pull_request_template.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Html ├── HTML elements-Block-level.md ├── Inline Elements.md ├── Margin and padding.md ├── comments.md ├── http.md └── index.html ├── Java ├── Interface │ ├── Animal.java │ ├── Cat.java │ ├── Dog.java │ └── main.java └── polymorphism.java ├── LICENSE ├── Python ├── Basics │ ├── Boolean_type.md │ ├── Comparisons.md │ ├── Naming_variables.md │ ├── PEP 8.md │ ├── README.md │ ├── comments.md │ ├── input.md │ └── operators.md ├── Conditions-(if-else-elif) │ ├── README.md │ ├── if.md │ └── if_else.md ├── Hello World │ └── hello-world.py ├── Indexes.md ├── Invoking_a_function.md ├── List.md ├── Loops │ ├── README.md │ └── while-loop.md ├── Program_with_numbers.md ├── README.md ├── desks_problem_code │ ├── README.md │ └── desks_problem.py ├── function.md ├── sum_of_digits │ ├── README.md │ └── the sum_of_digits.py └── variables.md └── README.md /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Click on '....' 17 | 3. Scroll down to '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Screenshots** 24 | If applicable, add screenshots to help explain your problem. 25 | 26 | **Desktop (please complete the following information):** 27 | - OS: [e.g. iOS] 28 | - Browser [e.g. chrome, safari] 29 | - Version [e.g. 22] 30 | 31 | **Smartphone (please complete the following information):** 32 | - Device: [e.g. iPhone6] 33 | - OS: [e.g. iOS8.1] 34 | - Browser [e.g. stock browser, safari] 35 | - Version [e.g. 22] 36 | 37 | **Additional context** 38 | Add any other context about the problem here. 39 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/custom.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Custom issue template 3 | about: Describe this issue template's purpose here. 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: feature 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | #### Issue Number 2 | ISSUE # 3 | 7 | 8 | #### Describe the changes you've made 9 | A clear and concise description of what you have done to successfully close your assigned issue. Any new files? or anything you feel to let us know! 10 | 11 | #### Describe if there is any unusual behaviour of your code(Write `NA` if there isn't) 12 | A clear and concise description of it. 13 | 14 | #### Additional context (OPTIONAL) 15 | Add any other context or screenshots about the feature request here. 16 | 17 | #### Test plan (OPTIONAL) 18 | A good test plan should give instructions that someone else can easily follow. 19 | How someone can test your code? 20 | 21 | #### Checklist 22 | 26 | - [ ] My code follows the code style of this project. 27 | - [ ] My change requires a change to the documentation. 28 | - [ ] I have updated the documentation accordingly. 29 | - [ ] I have read the **[CONTRIBUTING](https://github.com/GameofSource-GFG/Web-Development/blob/main/CONTRIBUTING.md)** document. 30 | - [ ] I have added tests to cover my changes. 31 | - [ ] All new and existing tests passed. 32 | - [ ] The title of my pull request is a short description of the requested changes. 33 | - [ ] Participating in SWOC2021. 34 | - [ ] Participating in DWOC. 35 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 |
99 |
100 |
101 | [](https://github.com/akrish4/Learn-By-Write)
102 |
103 |
Element p is also inside the div
9 |` defines a paragraph of text 34 | 35 | ``` 36 |
It's a paragraph of the text
37 |And this is another paragraph
38 | ``` 39 | 40 | ## `` defines a block of formatted text 41 | 42 | ``` 43 |44 | ........../> フ 45 | .........| _ _ | 46 | ......./` ミ_xノ 47 | ....../ | 48 | ...../ ヽ ノ 49 | ....│ | | | 50 | / ̄| | | | 51 | | ( ̄ヽ__ヽ_)__) 52 | .\二つ 53 |54 | ``` 55 | 56 | By default, any number of spaces occurring in the code in a row are shown as one on a web page. The `` tag displays the text as you want, with all spaces between characters 57 | 58 | 59 | ## `
` creates a horizontal line 60 | 61 | ``` 62 |
63 | ``` 64 | 65 | -------------------------------------------------------------------------------- /Html/Inline Elements.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Html/Margin and padding.md: -------------------------------------------------------------------------------- 1 | # Margin and padding 2 | - Margin is a distance from one package to another. In others words, it's an interblock space. 3 | 4 | - Padding refers to a shift from content to rim package. 5 | 6 | - Look at this simple picture below that illustrates the difference between these two terms: 7 | 8 |9 | -------------------------------------------------------------------------------- /Html/comments.md: -------------------------------------------------------------------------------- 1 | # Comments 2 | In HTML files, you can write a special text which the browser will ignore when opening the file. Such text is called a comment. Why write it if in the end it is simply and deliberately ignored? It actually makes a lot of sense to use comments during the process of development as they can greatly improve the readability of the code. 3 | 4 | A comment in HTML code can be generally defined as follows: 5 | 6 | 7 | ``` 8 | 9 | ``` 10 | Any text placed in will be considered a comment. The text of the comment can be placed either on a single line or several 11 | `Comments can be used anywhere on the page except the
tag.` 12 | 13 | # One-line comments 14 | Comments that occupy one line are called one-line comments. Take a careful look at the syntactic features of one-line comments: 15 | 16 | ``` 17 | 18 | 19 | 20 | 21 | 24 | 25 |One-line comments 22 | 23 |It's a paragraph of the text
26 | 27 | 28 | ``` 29 | 30 | The result in the browser will look as follows: 31 | 32 |33 | 34 | As you can see, the comment text is not displayed in the browser window. It is invisible for visitors of web pages, but can be very useful for developers. With these notes you can easily remember later why some particular code fragment is needed. Comments can also serve as hints for other developers who will work with your code later on. 35 | 36 | # Multi-line comments 37 | Comments that take up several lines are called multi-line comments. Take a look at their syntactic features: 38 | 39 | ``` 40 | 41 | 42 | 43 | 44 |
Multi-line comments 45 | 46 | 47 | 51 |It's a paragraph of the text
52 | 53 | 54 | ``` 55 | 56 | -------------------------------------------------------------------------------- /Html/http.md: -------------------------------------------------------------------------------- 1 | # HyperText Transfer Protocol 2 | 3 | ## What is HTTP? 4 | 5 | - Whenever you decide to check out your friends' pictures on social networks or leave them a message, watch a funny video with kittens, or find the meaning of an unknown term in the search engine, the device (client) from which you surf the Internet sends a request to the server and receives a response from it. 6 | 7 | - A client is a customer of a service, and a server is a computer that serves users or other computers. It can be located remotely, tens of thousands of kilometers away from you. The technology when the network load is distributed between servers and service customers is called Client-Server Architecture. 8 | 9 | 10 | 11 |12 | 13 | 14 | 15 | - Data exchange between the client and the server takes place due to the HTTP. HTTP stands for HyperText Transfer Protocol. A protocol is a set of rules and conventions that defines a uniform way to exchange data between different programs and handle errors. A Protocol is a set of specific rules by which programs or devices communicate with each other. Like a waiter in a restaurant, it accepts your requests, takes them to the server for processing, and then comes back to you with a response. 16 | 17 | - HTTP was developed by a scientist and CERN employee Tim Berners-Lee, the ideologist of the World Wide Web. 18 | - Work on the protocol continued for two years, and in March 1991, HTTP was used as a technology to help access documents on the Internet and facilitate hypertext navigation. This was Protocol version 0.9. It was designed to optimize communication between the client and the server on the network. After the appearance of the HTTP/0.9 standard, a separation of functions between the client and the server appeared during their interaction. 19 | 20 | - Currently, the most recent version of the Protocol is HTTP/2.0, released in 2015. Prior to that, HTTP/1.1 (released in 1999) was used for as long as sixteen years. This standard satisfied everyone for many years because of its innovations: with the advent of HTTP/1.1, the transfer of requests from client to server was greatly accelerated. 21 | 22 | - Not all sites have switched to HTTP/2 yet, but browsers support both new and old standards. 23 | 24 | ## What is HTTPS 25 | Despite the great functionality and popularity of HTTP, there is one drawback: data is transmitted in an unsecured form. This flaw can be critical when it comes to paying for purchases over the Internet or sending passport data: no user wants their confidential information to be leaked. 26 | 27 | To make the Internet space safer, the programmers decided to develop an add-on over HTTP, which helps to avoid data interception by encryption. The HTTP extension is called HTTPS and stands for HyperText Transfer Protocol Secure. 28 | 29 | HTTPS provides a secure connection between the user's browser and the web server. Often, the browser window displays a green address bar or lock indicating encrypted data transfer. You can also check the address bar of your web browser to see if you have a secure connection. It should start with "https://". 30 | -------------------------------------------------------------------------------- /Html/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 10 | 11 | 12 | 18 | 19 |
Hello, world! 20 | 21 | 22 | 23 | 24 | 25 | 26 | 31 | 36 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /Java/Interface/Animal.java: -------------------------------------------------------------------------------- 1 | package Interface; 2 | // Interfaces methods cannot have bodies to insure abstraction and to provide a set of protocols throughout the code 3 | public interface Animal { 4 | void eat(); 5 | void makeSound(); 6 | } 7 | -------------------------------------------------------------------------------- /Java/Interface/Cat.java: -------------------------------------------------------------------------------- 1 | package Interface; 2 | 3 | public class Cat implements Animal{ 4 | @Override 5 | public void eat() { 6 | System.out.println("I am a cat and I am eating"); 7 | } 8 | 9 | @Override 10 | public void makeSound() { 11 | System.out.println("Meow"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Java/Interface/Dog.java: -------------------------------------------------------------------------------- 1 | package Interface; 2 | 3 | public class Dog implements Animal{ 4 | @Override 5 | public void eat() { 6 | System.out.println("I am a dog and I am eating"); 7 | } 8 | 9 | @Override 10 | public void makeSound() { 11 | System.out.println("Barf! Barf!"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Java/Interface/main.java: -------------------------------------------------------------------------------- 1 | package Interface; 2 | 3 | public class main { 4 | public static void main(String[]args){ 5 | Cat cat = new Cat(); 6 | cat.eat(); 7 | cat.makeSound(); 8 | 9 | Dog dog = new Dog(); 10 | dog.eat(); 11 | dog.makeSound(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Java/polymorphism.java: -------------------------------------------------------------------------------- 1 | class Animal { 2 | public void animalSound() { 3 | System.out.println("The animal makes a sound"); 4 | } 5 | } 6 | 7 | class Pig extends Animal { 8 | public void animalSound() { 9 | System.out.println("The pig says: wee wee"); 10 | } 11 | } 12 | 13 | class Dog extends Animal { 14 | public void animalSound() { 15 | System.out.println("The dog says: bow wow"); 16 | } 17 | } 18 | 19 | class MyMainClass { 20 | public static void main(String[] args) { 21 | Animal myAnimal = new Animal(); 22 | Animal myPig = new Pig(); 23 | Animal myDog = new Dog(); 24 | 25 | myAnimal.animalSound(); 26 | myPig.animalSound(); 27 | myDog.animalSound(); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Ananthakrishnan Nair RS 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Python/Basics/Boolean_type.md: -------------------------------------------------------------------------------- 1 | # Boolean type 2 | The Boolean type, or simply bool, is a special data type that has only two possible values: True and False. In Python the names of boolean values start with a capital letter. 3 | 4 | In programming languages the boolean, or logical, type is a common way to represent something that has only two opposite states like on or off, yes or no, etc. 5 | 6 | If you are writing an application that keeps track of door openings, you'll find it natural to use bool to store the current door state. 7 | 8 | is_open = True 9 | is_closed = False 10 | 11 | print(is_open) # True 12 | print(is_closed) # False 13 | # Boolean operations 14 | There are three built-in boolean operators in Python: and, or and not. The first two are binary operators which means that they expect two arguments. not is a unary operator, it is always applied to a single operand. First, let's look at these operators applied to the boolean values. 15 | - There are three built-in boolean operators in Python: and, or and not. The first two are binary operators which means that they expect two arguments. not is a unary operator, it is always applied to a single operand. First, let's look at these operators applied to the boolean values. 16 | 17 | a = True and True # True 18 | b = True and False # False 19 | c = False and False # False 20 | d = False and True # False 21 | - or is a binary operator, it returns True if at least one argument is true, otherwise, it returns False. 22 | 23 | a = True or True # True 24 | b = True or False # True 25 | c = False or False # False 26 | d = False or True # True 27 | - not is a unary operator, it reverses the boolean value of its argument. 28 | 29 | to_be = True # to_be is True 30 | not_to_be = not to_be # not_to_be is False 31 | # The precedence of boolean operations 32 | Logical operators have a different priority and it affects the order of evaluation. Here are the operators in order of their priorities: not, and, or. So, not is considered first, then and, finally or. Having this in mind, consider the following expression: 33 | 34 | print(False or not False) # True 35 | 36 | First, the part not False gets evaluated, and after evaluation, we are left with False or True. This results in True, if you recall the previous section. 37 | 38 | While dealing solely with the boolean values may seem obvious, the precedence of logical operations will be quite important to remember when you start working with so-called truthy and falsy values. 39 | # Truthy and falsy values 40 | Though Python has the boolean data type, we often want to use non-boolean values in a logical context. And Python lets you test almost any object for truthfulness. When used with logical operators, values of non-boolean types, such as integers or strings, are called truthy or falsy. It depends on whether they are interpreted as True or False. 41 | The following values are evaluated to False in Python: 42 | 43 | - constants defined to be false: None and False, 44 | - zero of any numeric type: 0, 0.0, 45 | - empty sequences and containers: "", [], {}. 46 | Anything else generally evaluates to True. Here is a couple of examples: 47 | 48 | print(0.0 or False) # False 49 | print("True" and True) # True 50 | print("" or False) # False 51 | Generally speaking, and and or could take any arguments that can be tested for a boolean value. 52 | 53 | Now we can demonstrate more clearly the difference in operator precedence: 54 | ```python 55 | # `and` has a higher priority than `or` 56 | truthy_integer = False or 5 and 100 # 100 57 | ``` 58 | Again, let's break the above expression into parts. Since the operator and has a higher priority than or, we should look at the 5 and 100 part. Both 100 and 5 happen to be truthy values, so this operation will return 100. You have never seen this before, so it's natural to wonder why we have a number instead of the True value here. We'll cover this surprising fact shortly. Coming back to the original expression, you can see that the last part False or 100 does exactly the same thing, returns 100 instead of True. 59 | ``` 60 | The operators or and and return one of their operands, not necessarily of the boolean type. Nonetheless, not always returns a boolean value. 61 | ``` 62 | Another tricky example is below: 63 | 64 | ```python 65 | tricky = not (False or '') # True 66 | ``` 67 | A pair of parentheses is a way to specify the order in which the operations are performed. Thus, we evaluate this part of the expression first: False or ''. This operand '' evaluates to False and or returns this empty string. Since the result of the enclosed expression is negated, we get True in the end: not '' is the same as True. Why didn't we get, say, a non-empty string? The not operator creates a new value, which by default has the boolean type. So, as stated earlier, the unary operator always returns a logical value. 68 | # Short-circuit evaluation 69 | The last thing to mention is that logical operators in Python are short-circuited. That's why they are also called lazy. That means that the second operand in such an expression is evaluated only if the first one is not sufficient to evaluate the whole expression. 70 | 71 | - x and y returns x if x is falsy; otherwise, it evaluates and returns y. 72 | 73 | - x or y returns x if x is truthy; otherwise, it evaluates and returns y. 74 | For instance: 75 | ```python 76 | # division is never evaluated, because the first argument is True 77 | lazy_or = True or (1 / 0) # True 78 | 79 | # division is never evaluated, because the first argument is False 80 | lazy_and = False and (1 / 0) # False 81 | ``` 82 | Those were the very basics of boolean values and logical operations in Python. It's definitely good to know them right from the beginning! 83 | -------------------------------------------------------------------------------- /Python/Basics/Comparisons.md: -------------------------------------------------------------------------------- 1 | # Comparison operators 2 | Comparison or relation operations let you compare two values and determine the relation between them. There are ten comparison operators in Python: 3 | - `<` strictly less than 4 | - `<=` less than or equal 5 | - `>` strictly greater than 6 | - `>=` greater than or equal 7 | - `==` equal 8 | - `!=` not equal 9 | - `is` object identity 10 | - `is not` negated object identity 11 | - `in` membership 12 | - `not in` negated membership 13 | 14 | The result of applying these operators is always bool. 15 | # Comparing integers 16 | only integer comparison. 17 | 18 | ```python 19 | a = 5 20 | b = -10 21 | c = 15 22 | 23 | result_1 = a < b # False 24 | result_2 = a == a # True 25 | result_3 = a != b # True 26 | result_4 = b >= c # False 27 | ``` 28 | Any expression that returns integer is a valid comparison operand too: 29 | 30 | ```python 31 | calculated_result = a == b + c # True 32 | ``` 33 | Given the defined variables a, b and c, we basically check if 5 is equal to -10 + 15, which is true. 34 | # Comparison chaining 35 | Since comparison operations return boolean values, you can join them using logical operators. 36 | 37 | ``` 38 | x = -5 39 | y = 10 40 | z = 12 41 | 42 | result = x < y and y <= z # True 43 | ``` 44 | In Python, there is a fancier way to write complex comparisons. It is called chaining. For example, x < y <= z is almost equivalent to the expression you saw in the last example. The difference is that y is evaluated only once. 45 | 46 | > result = 10 < (100 * 100) <= 10000 # True, the multiplication is evaluated once 47 | -------------------------------------------------------------------------------- /Python/Basics/Naming_variables.md: -------------------------------------------------------------------------------- 1 | # Naming variables 2 | - As you know, every variable has a name that uniquely identifies it among other variables. Giving a good name to 3 | a variable may not be as simple as it seems. Experienced programmers are careful when naming their variables to ensure that their programs are easy to 4 | understand. It is important because programmers spend a lot of time reading and understanding code written by other programmers. If variables have bad names, even your own code 5 | will seem unclear to you in a few months. 6 | ## Code style convention 7 | PEP 8 provides some rules for variable names to increase code readability. 8 | - #### Use lowercase and underscores to split words. Even if it's an abbreviation 9 | 10 | 11 | http_response # yes! 12 | httpresponse # no 13 | myVariable # no, that's from Java 14 | However, if you want to define a constant, it's common to write its name in all capital letters and, again, separate words with underscores. Normally, constants are stored in special files called modules. 15 | 16 | 17 | SPEED_OF_LIGHT = 299792458 18 | 19 | - #### Avoid names of one letter that could be easily mixed up with numbers like 'l' (lowercase letter el), 'O' (uppercase letter oh), or 'I' (uppercase letter eye). 20 | 21 | l = 1 # no 22 | O = 100 # no, if you use this variable name further in your code it would look like zero 23 | 24 | - #### Although you can use any Unicode symbols, the code style convention recommends limiting variable names with ASCII characters. 25 | 26 | # Using Cyrillic instead of Latin can cause an evening of useless headache 27 | # These are different variables! 28 | copy = "I'm written in Latin alphabet" # yes! 29 | сору = "And I'm written in Cyrillic!" # no 30 | - #### If the most suitable variable name is some Python keyword, add an underscore to the end of it. 31 | 32 | class_ = type(var) # yes! 33 | klass = type(var) # no 34 | ## Other variable names best practices 35 | There are also some best practices that are common for many programming languages. 36 | 37 | - Choose a name that makes sense. The variable name must be readable and descriptive and should explain to the reader what sort of values will be stored in it. 38 | 39 | score # yes! 40 | s # no 41 | 42 | count # yes! 43 | n # no 44 | - Don't use too generic names. Try to choose a name that will explain the meaning of the variable. But don't make it too wordy. 1-3 words are usually enough. 45 | 46 | http_response # yes! 47 | var1 # no 48 | http_response_from_the_server # no, some words can be dropped 49 | - If a word is long, try to find the most common and expected short form to make it easier to guess later. 50 | 51 | output_file_path # yes! 52 | fpath # no 53 | output_flpth # no 54 | - Avoid names from the built-in types list. 55 | 56 | str = 'Hello!' # no, because in the further code you can't use str type as it's overridden 57 | -------------------------------------------------------------------------------- /Python/Basics/PEP 8.md: -------------------------------------------------------------------------------- 1 | # PEP 8 2 | - How to write code that is clean and easy to read? That is the question that you bump into when moving from simple single-line programs to more 3 | complicated ones. In the beginning, it may seem unimportant, but in real life, programming is a process that involves a lot of people that work together, so you 4 | spend more time reading code than writing it. 5 | - Although Python is often more readable than other programming languages because of its minimalistic syntax, just syntax itself is not enough. 6 | It is the way you write code that affects general readability. That is why you need to follow common conventions about 7 | programming style, so other programmers could read your code easily. 8 | - You may ask, where do these conventions come from? There is a document that is called PEP 8. The key idea of it is to use the same code 9 | style for all python projects as if they were written by the same programmer. This document guarantees that even a beginner will easily understand the code, written 10 | by any other developer. 11 | # 1.PEPs 12 | - Before going further, let’s talk about PEP for a moment. PEP stands for *Python Enhancement Proposal*. There are different types of PEP and the most useful one for beginners is the informational PEP. PEPs of this kind typically describe commonly accepted guidelines or conventions about the language, so they can be very helpful. Besides PEP 8, which is an official style guide, another great PEP to look at is the ***Zen of Python***. 13 | # 2.The length of a line 14 | - Do not use more than 79 characters in a line of code. Shorter lines look better in code editors. During this course, we will learn several ways to achieve it. 15 | # 3.Avoid extra spaces 16 | - Sometimes you may add some spaces even if you don't really need it. This will reduce the readability of your code. 17 | ### Avoid extra spaces within parentheses. 18 | 19 | ###### Good 20 | print('Hello!') 21 | ###### Bad 22 | print( 'Hello!' ) 23 | ### Avoid an extra space before an open parenthesis. 24 | ###### Good 25 | print('some text') 26 | ###### Bad 27 | print ('some text') 28 | # 4.Quotes 29 | - you can use either single or double quotes to define strings. Please, choose one that you like the most and consistently use it in your code. The only recommendation in PEP 8 is that if a string contains single or double quotes, you should use the other one to avoid backslashes. 30 | ###### Good 31 | print("It's a good string!") 32 | ###### Bad 33 | print('It\'s a bad string!') 34 | -------------------------------------------------------------------------------- /Python/Basics/README.md: -------------------------------------------------------------------------------- 1 | # Basics 2 | -------------------------------------------------------------------------------- /Python/Basics/comments.md: -------------------------------------------------------------------------------- 1 | # What is a comment? 2 | - Sometimes you need to explain what some particular parts of your code are for. Lucky you, since Python gives you an 3 | opportunity to fulfill your needs. You can leave special notes called comments. 4 | - Python comments start with a hash #. Everything after the hash mark and up to the end of the line is regarded as a comment and will be ignored when running the code. 5 | 6 | 7 | 8 | 9 | print("This will run.") # This won't run 10 | 11 | -------------------------------------------------------------------------------- /Python/Basics/input.md: -------------------------------------------------------------------------------- 1 | # Taking input 2 | - Sometimes programs need to interact with users, either to receive some data or to deliver some sort of result. And that's when the input() function steals the show. 3 | 4 | # Reading input from a user 5 | - The input data we want to get is nothing but some value entered by the user. 6 | The input() function reads this value and returns it in a program as a string. For example, the following program reads the user name and prints a greeting 7 | 8 | 9 | user_name = input() 10 | print('Hello, ' + user_name) 11 | -------------------------------------------------------------------------------- /Python/Basics/operators.md: -------------------------------------------------------------------------------- 1 | # Integer arithmetic 2 | - In real life, we often perform arithmetic operations. They help us to calculate the change from a purchase, determine the area of a room, count the number of people in a line, and so on. The same operations are used in programs. 3 | 4 | ## Basic operations 5 | #### Python supports basic arithmetic operations 6 | - addition + 7 | - subtraction - 8 | - multiplication * 9 | - division / 10 | - integer division // 11 | #### The examples below show how it works for numbers. 12 | 13 | print(10 + 10) # 20 14 | print(100 - 10) # 90 15 | print(10 * 10) # 100 16 | print(77 / 10) # 7.7 17 | print(77 // 10) # 7 18 | #### There is a difference between division / and integer division //. The first produces a floating-point number (like 7.7), while the second one produces an integer value (like 7) ignoring the decimal part. 19 | 20 | ##### Python raises an error if you try to divide by zero. 21 | 22 | ZeroDivisionError: division by zero 23 | ## Writing complex expressions 24 | Arithmetic operations can be combined to write more complex expressions: 25 | 26 | print(2 + 2 * 2) # 6 27 | Like in arithmetic, parentheses can be nested inside each other. You can also use them for clarity. 28 | 29 | The minus operator has a unary form that negates the value or expression. A positive number becomes negative, and a negative number becomes positive 30 | 31 | print(-10) # -10 32 | print(-(100 + 200)) # -300 33 | print(-(-20)) # 20 34 | ## Other operations 35 | The remainder of a division. Python modulo operator % is used to get the remainder of a division. It may come in handy when you want to check if a number is even. Applied to 2, it returns 1 for odd numbers and 0 for the even ones. 36 | 37 | print(7 % 2) # 1, because 7 is an odd number 38 | print(8 % 2) # 0, because 8 is an even number 39 | #### Here are some more examples: 40 | 41 | # Divide the number by itself 42 | print(4 % 4) # 0 43 | # At least one number is a float 44 | print(11 % 6.0) # 5.0 45 | # The first number is less than the divisor 46 | print(55 % 77) # 55 47 | # With negative numbers, it preserves the divisor sign 48 | print(-11 % 5) # 4 49 | print(11 % -5) # -4 50 | ***Taking the remainder of the division by 0 also leads to ZeroDivisionError.*** 51 | ##### The behavior of the mod function in Python might seem unexpected at first glance. While 11 % 5 = 1 and -11 % -5 = -1 when both numbers on the left are of the same sign, 11 % -5 = -4 and -11 % 5 = 4 if we have one negative number. The thing is, in Python, the remainder always has the same sign as the divisor. 52 | ##### In the first case, 11 % -5 = -4, as the remainder should be negative, we need to compare 15 and 11, not 10 and 11: 11 = (-5) * (-3) + (-4). In the second case, -11 % 5 = 4, the remainder is supposed to be positive: -11 = 5 * (-3) + 4. 53 | #### Exponentiation. Here is a way to raise a number to a power: 54 | 55 | print(10 ** 2) # 100 56 | **This operation has a higher priority over multiplication.** 57 | -------------------------------------------------------------------------------- /Python/Conditions-(if-else-elif)/README.md: -------------------------------------------------------------------------------- 1 | # Conditionals 2 | -------------------------------------------------------------------------------- /Python/Conditions-(if-else-elif)/if.md: -------------------------------------------------------------------------------- 1 | # if statement 2 | There are situations when your program needs to execute some piece of the code only if a particular condition is true. Such a piece of the code should be placed within the body of an if statement. The pattern is the same as in the English language: first comes the keyword if , then a condition, and then a list of expressions to execute. The condition is always a Boolean expression, that is, its value equals either True or False. Here is one example of how the code with a conditional expression should look like: 3 | 4 | ```python 5 | biscuits = 17 6 | if biscuits >= 5: 7 | print("It's time for tea!") 8 | ``` 9 | 10 | Note that the condition ends with a colon and a new line starts with an indentation. Usually, 4 spaces are used to designate each level of indentation. A piece of code in which all lines are on the same level of indentation is called a block of code. In Python, only indentation is used to separate different blocks of code, hence, only indentation shows which lines of code are supposed to be executed when the if statement is satisfied, and which ones should be executed independently of the if statement. Check out the following example: 11 | 12 | ```python 13 | if biscuits >= 5: 14 | print("It's time for tea!") 15 | print("What tea do you prefer?") 16 | print("What about some chocolate?") 17 | ``` 18 | In this example, the line "It's time for tea!", as well as "What tea do you prefer?", will be printed only if there are 5 or more biscuits. The line "What about some chocolate?" will be printed regardless of the number of biscuits. 19 | 20 | An if statement is executed only if its condition holds (the Boolean value is True), otherwise, it's skipped. 21 | 22 | Boolean values basically make it clear whether a piece of code needs to be executed or not. Since comparisons result in bool, it's always a good idea to use them as a condition. 23 | 24 | ## Nested if statement 25 | Sometimes a condition happens to be too complicated for a simple if statement. In this case, you can use so-called nested if statements. The more if statements are nested, the more complex your code gets, which is usually not a good thing. However, this doesn't mean that you need to avoid nested if statements at all costs. Let's take a look at the code below: 26 | 27 | ```python 28 | rainbow = "red, orange, yellow, green, blue, indigo, violet" 29 | warm_colors = "red, yellow, orange" 30 | my_color = "orange" 31 | 32 | if my_color in rainbow: 33 | print("Wow, your color is in the rainbow!") 34 | if my_color in warm_colors: 35 | print("Oh, by the way, it's a warm color.") 36 | ``` 37 | 38 | The example above illustrates a nested if statement. If the variable my_color is a string that contains the name of a color from the rainbow, we enter the body of the first if statement. First, we print the message and then check if our color belongs to the warm colors. The membership operator in simply shows whether my_color is a substring of the respective string, rainbow or warm_colors. Just like arithmetic comparisons, it returns a boolean value. 39 | 40 | Here is what we will see in our case: 41 | 42 | ```python 43 | Wow, your color is in the rainbow! 44 | Oh, by the way, it's a warm color. 45 | ``` 46 | When it comes to nested if statements, proper indentation is crucial, so do not forget to indent each statement that starts with the if keyword. 47 | 48 | -------------------------------------------------------------------------------- /Python/Conditions-(if-else-elif)/if_else.md: -------------------------------------------------------------------------------- 1 | # if-else 2 | An if-else statement is another type of conditional expressions in Python. It differs from an if statement by the presence of the additional keyword else. The block of code that else contains executes when the condition of your if statement does not hold (the Boolean value is False). Since an else statement is an alternative for an if statement, only one block of code can be executed. Also, else doesn't require any condition: 3 | 4 | if today == "holiday": 5 | print("Lucky you!") 6 | else: 7 | print("Keep your chin up, then.") 8 | Note that the 4-space indentation rule applies here too. 9 | 10 | As you may soon find out, programmers do like all sorts of shortcuts. For conditional expressions there's a trick as well – you can write an if-else statement in one line. This is called a ternary operator and looks like this: 11 | 12 | print("It’s a day now!" if sun else "It’s a night for sure!") 13 | Or, more generally: 14 | 15 | first_alternative if condition else second_alternative 16 | It's a matter of convenience, but remember that the code you create should still be readable. 17 | 18 | Nested if-else 19 | It should be mentioned, that if-else statements can be nested the same way as if statements. An additional conditional expression may appear after the if section as well as after the else section. Once again, don't forget to indent properly: 20 | 21 | if x < 100: 22 | print('x < 100') 23 | else: 24 | if x == 100: 25 | print('x = 100') 26 | else: 27 | print('x > 100') 28 | print('This will be printed only because x >= 100') 29 | Now you are ready not only to set conditions but also to consider different alternatives. Congratulations! 30 | -------------------------------------------------------------------------------- /Python/Hello World/hello-world.py: -------------------------------------------------------------------------------- 1 | # to print hello world , just print it using the print statement. 2 | print("hello world") 3 | # it's that simple. 4 | a='ark' 5 | b='anant' 6 | print(f'this is f strings{b} by {a}') 7 | -------------------------------------------------------------------------------- /Python/Indexes.md: -------------------------------------------------------------------------------- 1 | There are several types of collections to store data in Python. Positionally ordered collections of elements are usually called sequences, and both lists and strings 2 | \belong to them. Each element in a list, as well as each character in a string, has an index that corresponds to its position. Indexes are used to access elements 3 | within a sequence. Indexing is zero-based, so if you see a person who counts from zero, 4 | you must have met a programmer. 5 | 6 | # Indexes of elements 7 | To access an element of a list by its index, you need to use square brackets. 8 | You add the brackets after the list and, between them, you write the index of an element you want to get. 9 | 10 | Don't forget, the indexes start at 0, so the index of the first element is 0. The index of the last element is equal to `len(list) - 1`. 11 | 12 | Let's take a look at the example below: 13 | 14 | ```python 15 | colors = ['red', 'green', 'blue'] 16 | 17 | first_elem = colors[0] # 'red' 18 | second_elem = colors[1] # 'green' 19 | third_elem = colors[2] # 'blue' 20 | ``` 21 | 22 | Strings work in the same way: 23 | 24 | ```python 25 | pet = "cat" 26 | 27 | first_char = pet[0] # 'c' 28 | second_char = pet[1] # 'a' 29 | third_char = pet[2] # 't' 30 | ``` 31 | 32 | # Potential pitfalls 33 | When using indexes, it's important to stay within the range of your sequence: you'll get an error (called IndexError) if you try to access an element with a non-existing index! 34 | 35 | ```python 36 | colors = ['red', 'green', 'blue'] 37 | pet = "cat" 38 | 39 | print(colors[3]) # IndexError: list index out of range 40 | print(pet[3]) # IndexError: string index out of range 41 | ``` 42 | 43 | There is one more obstacle in your way. Imagine that you want to change one of the elements in a list. It can be easily done: 44 | 45 | ```python 46 | colors = ['red', 'green', 'blue'] 47 | 48 | colors[1] = 'white' 49 | print(colors) # ['red', 'white', 'blue'] 50 | ``` 51 | 52 | # Negative indexes 53 | 54 | The easier way to access the elements at the end of a list or a string is to use negative indexes: the minus before the number changes your perspective in a way and you look at the sequence from the end. So, the last element of a list, in this case, has the index equal to -1, and the first element of the list has the index `-len(list)` (the length of the list). 55 | 56 | For example: 57 | 58 | ```python 59 | colors = ['red', 'green', 'blue'] 60 | 61 | last_elem = colors[-1] # 'blue' 62 | second_elem = colors[-2] # 'green' 63 | first_elem = colors[-3] # 'red' 64 | 65 | pet = "cat" 66 | 67 | last_char = pet[-1] # 't' 68 | second_char = pet[-2] # 'a' 69 | first_char = pet[-3] # 'c' 70 | ``` 71 | 72 | As you can see, it works the same for lists and strings. 73 | 74 | > If you write a non-existing negative index, you'll also get IndexError. Be careful with indexes to avoid off-by-one errors in your code. 75 | 76 | The following picture shows the general concept of indexes in a list: 77 | 78 |79 | 80 | 81 | Since you have learned the concept of indexes, we hope that from now on you will not encounter any difficulties when using them! 82 | -------------------------------------------------------------------------------- /Python/Invoking_a_function.md: -------------------------------------------------------------------------------- 1 | # Invoking a function🤔😃 2 | Even though invoking functions in Python is not about casting a spell or the like, it does sometimes work wonders. Let's start with the concept. Basically, a function is a structured fragment of code we may want to use in more than one place and more than one time. For another thing, functions allow us to read both our code and that of someone else way better. Haven't they become your favorite yet? 3 | 4 | Here is a simple function call: 5 | 6 | `multiply(1, 7)` 7 | 8 | Here multiply is the name of the function, and numbers in parentheses (1, 7) are its arguments. What is an argument? Well, it's just a value, that will be used inside the body of the function. Let's go deeper into it! 9 | 10 | 11 | 12 | # Invoking print() 13 | To call, or invoke, a function in your program, simply write its name and add parentheses after it. That's all it takes! Fun fact: if you've ever typed an expression like this `print("Hello, world!")`, you already know a little about functions. In this tiny example, however, we see the message `"Hello, world!"` in the parentheses after the name of the `print` function. What does it mean? This string is just an argument. And more often than not, functions do have arguments. As for the `print` function, we may as well use it with no argument at all or even with multiple arguments: 14 | 15 | 16 | 17 | ```python 18 | print("Hello, world!") 19 | print() 20 | print("Bye,", "then!") 21 | ``` 22 | 23 | And here is the output: 24 | 25 | ```python 26 | Hello, world! 27 | 28 | Bye, then! 29 | ``` 30 | 31 | 32 | So, the first call prints one string, the second call of `print` without arguments prints, in fact, an empty line, and the last call outputs our two messages as one expression. Are you surprised by these results? Then you may learn how the `print` function works in more detail from its documentation. The Python documentation contains all sorts of information about the function of your interest, for example, which arguments it expects. 33 | 34 | 35 | 36 | 37 | # Built-in functions 38 | Functions can make life easier, provided one is aware of their existence. Many algorithms are already written, so there is no need for reinvention, except perhaps for educational purposes. The Python interpreter has a number of functions and types built into it, so they are always available. Currently, the number of built-in functions amounts to 69 (in the latest version Python 3.8). Some of them are used to convert the object type, for example, str() returns a string, int() returns an integer, float() returns a floating-point number. Others deal with numbers: you can round() them and sum() them, find the minimum min() or the maximum max(). Still others give us information about the object: its type() or length len(). Let's consider them in action 39 | 40 | In the following example, len() counts the number of characters in the string (the same goes for any sequence). 41 | 42 | ```python 43 | number = "111" 44 | 45 | # finding the length of an object 46 | print(len(number)) # 3 47 | ``` 48 | 49 | Then we declare the variables integer and float_number and write their sum into my_sum. By the way, the sum() function also deals with sequences. 50 | 51 | ```python 52 | # converting types 53 | integer = int(number) 54 | float_number = float(number) 55 | print(str(float_number)) # "111.0" 56 | 57 | # adding and rounding numbers 58 | my_sum = sum((integer, float_number)) 59 | ``` 60 | 61 | The result is a floating-point number, which becomes clear after printing `my_sum`. 62 | 63 | 64 | ```python 65 | print(my_sum) # 222.0 66 | print(round(my_sum)) # 222 67 | ``` 68 | 69 | Furthermore, you can see how to find the minimum and maximum values: the smallest number equals 111 and the largest number 222.0 belongs to floats . 70 | 71 | 72 | ```python 73 | # finding the minimum and the maximum 74 | print(min(integer, float_number)) # 111 75 | print(type(max(integer, float_number, my_sum))) #
76 | ``` 77 | 78 | # Recap 79 | 80 | Let's make a brief summary: 81 | 82 | - functions are meant to be reusable, which means we are free to apply it multiple times with different arguments, 83 | - to call a function write its name followed by parentheses and place the arguments within, 84 | - normally, a function has documentation, which sometimes might be of huge help. 85 | 86 | -------------------------------------------------------------------------------- /Python/List.md: -------------------------------------------------------------------------------- 1 | # List 2 | In your programs, you often need to group several elements in order to process them as a single object. For this, you will need to use different collections. One of the most useful collections in Python is a list. It is one of the most important things in Python. 3 | 4 | ## Creating and printing lists 5 | Look at a simple list that stores several names of dogs' breeds: 6 | 7 | ```python 8 | dog_breeds = ['corgi', 'labrador', 'poodle', 'jack russell'] 9 | print(dog_breeds) # ['corgi', 'labrador', 'poodle', 'jack russell'] 10 | ``` 11 | In the first line, we use square brackets to create a list that contains four elements and then assign it to the `dog_breeds` variable. In the second line, the list is printed through the variable's name. All the elements are printed in the same order as they were stored in the list because lists are **ordered**. 12 | 13 | Here is another list that contains five integers: 14 | ```python 15 | numbers = [1, 2, 3, 4, 5] 16 | print(numbers) # [1, 2, 3, 4, 5] 17 | ``` 18 | 19 | Another way to create a list is to invoke the list function. It is used to create a list out of an iterable object: that is, a kind of object where you can get its elements one by one. The concept of iterability will be explained in detail further on, but let's look at the examples below: 20 | 21 | ```python 22 | list_out_of_string = list('danger!') 23 | print(list_out_of_string) # ['d', 'a', 'n', 'g', 'e', 'r', '!'] 24 | 25 | list_out_of_integer = list(235) # TypeError: 'int' object is not iterable 26 | ``` 27 | So, the list function creates a list containing each element from the given iterable object. For now, remember that a string is an example of an iterable object, and an integer is an example of a non-iterable object. A list itself is also an iterable object. 28 | 29 | Let's also note the difference between the list function and creating a list using square brackets: 30 | ```python 31 | multi_element_list = list('danger!') 32 | print(multi_element_list) # ['d', 'a', 'n', 'g', 'e', 'r', '!'] 33 | 34 | single_element_list = ['danger!'] 35 | print(single_element_list) # ['danger!'] 36 | ``` 37 | The square brackets and the list function can also be used to create empty lists that do not have elements at all. 38 | 39 | ```python 40 | empty_list_1 = list() 41 | empty_list_2 = [] 42 | ``` 43 | In the following topics, we will consider how to fill empty lists. 44 | 45 | ## Features of lists 46 | Lists can store duplicate values as many times as needed. 47 | ```python 48 | on_off_list = ['on', 'off', 'on', 'off', 'on'] 49 | print(on_off_list) # ['on', 'off', 'on', 'off', 'on'] 50 | ``` 51 | 52 | Another important thing about lists is that they can contain different types of elements. So there are neither restrictions, nor fixed list types, and you can add to your list any data you want, like in the following example: 53 | ```python 54 | different_objects = ['a', 1, 'b', 2] 55 | ``` 56 | 57 | ## Length of a list 58 | 59 | Sometimes you need to know how many elements are there in a list. There is a built-in function called len that can be applied to any iterable object, and it returns simply the length of that object 60 | 61 | So, when applied to a list, it returns the number of elements in that list. 62 | ```python 63 | numbers = [1, 2, 3, 4, 5] 64 | print(len(numbers)) # 5 65 | 66 | empty_list = list() 67 | print(len(empty_list)) # 0 68 | 69 | single_element_list = ['danger!'] 70 | print(len(single_element_list)) # 1 71 | 72 | multi_elements_list = list('danger!') 73 | print(len(multi_elements_list)) # 7 74 | ``` 75 | 76 | In the example above, you can see how the len() function works. Again, pay attention to the difference between list() and [] as applied to strings: it may not result in what you expected. 77 | 78 | ## Recap 79 | As a recap, we note that lists are: 80 | - ordered, i.e. each element has a fixed position in a list; 81 | - iterable, i.e. you can get their elements one by one; 82 | - able to store duplicate values; 83 | - able to store different types of elements. 84 | -------------------------------------------------------------------------------- /Python/Loops/README.md: -------------------------------------------------------------------------------- 1 | # Loops 2 | -------------------------------------------------------------------------------- /Python/Loops/while-loop.md: -------------------------------------------------------------------------------- 1 | # While loop 2 | Sometimes one iteration (=execution) of a statement is not enough to get the result you need. That is why Python offers a special statement that will execute a block of code several times. Meet the loop command and one of the universal loops — the while loop. 3 | 4 | People generally don't choose Python to write fast code. The main advantages of Python are readability and simplicity. As the while loop requires the introduction of extra variables, iteration takes up more time. Thus, the while loop is quite slow and not that popular. It resembles a conditional operator: using the while loop, we can execute a set of statements as long as the condition is true. 5 | 6 | The condition itself (2) is written before the body of the loop (some call it the conditional code) and is checked before the body is executed. If the condition is true (3a), the iterations continue. If the condition is false (3b), the loop execution is terminated and the program control moves further to the next operation. 7 | ## Visualization 8 | If we visualize the while loop, it’ll look like this 9 | 10 | ```python 11 | number = 0 12 | while number < 5: 13 | print(number) 14 | number += 1 15 | print('Now, the number is equal to 5') 16 | ``` 17 | The variable number plays here the role of a counter – a variable that changes its value after each iteration. In this case, the iterations continue until the number is equal to 5 (note that the program outputs the value of the number before increasing it). When the value of a counter reaches 5, the program control moves to the next operation and prints the message. Here you can see the output of this code: 18 | 19 | ```python 20 | 0 21 | 1 22 | 2 23 | 3 24 | 4 25 | Now, the number is equal to 5 26 | ``` 27 | ## The infinite loop 28 | If you delete a part of the conditional code where you increase the value of a counter, you will bump into the infinite loop. What does it mean? Since you don’t increase your variable, a condition never becomes false and can work forever. Usually, it is a logical fallacy, and you'll have to stop the loop using special statements or finishing the loop manually. 29 | 30 | Sometimes the infinite loop can be useful, e.g. in querying a client when the loop works continuously to provide the constant exchange of information with a user. You can implement it by writing True as a condition after the while header. 31 | 32 | ```python 33 | while True: 34 | ... 35 | ``` 36 | 37 | Now you are familiar with the while loop and its usage. Don’t forget about the role of a counter, otherwise, you’ll have to deal with the infinite loop. After you’ve written the code, try to "run" it as if you were a Python program. That’ll help you understand how the loop works. 38 | 39 | Programming is all about simplification, so the code should be readable, short, and clear. Don’t forget about comments and syntax. In the beginning, it may seem that the while loop is not that easy to implement, but after a couple of times, you’ll see that it’s a very useful tool. 40 | -------------------------------------------------------------------------------- /Python/Program_with_numbers.md: -------------------------------------------------------------------------------- 1 | # Program with numbers 2 | ## Reading numbers from user input 3 | You might have become familiar with the input() function in Python, it's hardly new to you that any data passed to this function is treated as a string. But how should we deal with numerical values? As a general rule, they are explicitly converted to corresponding numerical types: 4 | 5 | integer = int(input()) 6 | floating_point = float(input()) 7 | ## Free air miles 8 | Imagine you have a credit card with a free air miles bonus program (or maybe you already have one). As a user, you are expected to input the amount of money you spend on average from this card per month. Let's assume that the bonus program gives you 2 free air miles for every dollar you spend. Here's a simple program to figure out when you can travel somewhere for free: 9 | 10 | # the average amount of money per month 11 | money = int(input("How much money do you spend per month: ")) 12 | 13 | # the number of miles per unit of money 14 | n_miles = 2 15 | 16 | # earned miles 17 | miles_per_month = money * n_miles 18 | 19 | # the distance between London and Paris 20 | distance = 215 21 | 22 | # how many months do you need to get 23 | # a free trip from London to Paris and back 24 | print(distance * 2 / miles_per_month) 25 | 26 | This program will calculate how many months you need to travel the selected distance and back. 27 | ## Advanced forms of assignment 28 | Whenever you use an equal sign =, you actually assign some value to a name. For that reason, = is typically referred to as an assignment operator. Meanwhile, there are other assignment operators you can use in Python. They are also called compound assignment operators, for they carry out an arithmetic operation and assignment in one step. Have a look at the code snippet below: 29 | 30 | # simple assignment 31 | number = 10 32 | number = number + 1 # 11 33 | This code is equivalent to the following one: 34 | 35 | # compound assignment 36 | number = 10 37 | number += 1 # 11 38 | One can clearly see from the example that the second piece of code is more concise (for it doesn't repeat the variable's name). 39 | 40 | Naturally, similar assignment forms exist for the rest of arithmetic operations: -=, *=, /=, //=, %=, **=. Given the opportunity, use them to save time and effort. 41 | ## Counter variable 42 | 43 | In programming, loops are used alongside special variables called counters. A counter counts how many times a particular code is run. It also follows that counters should be integers. Now we are getting to the point: you can use the operators += and -= to increase or decrease the counter respectively. 44 | 45 | counter = 1 46 | step = int(input()) # let it be 3 47 | counter += step 48 | print(counter) # it should be 4, then 49 | In case you need only non-negative integers from the user (we are increasing the counter after all!), you can prevent incorrect inputs by using the abs() function. It is a Python built-in function that returns the absolute value of a number (that is, value regardless of its sign). Let's readjust our last program a bit: 50 | 51 | counter = 1 52 | step = abs(int(input())) # user types -3 53 | counter += step 54 | print(counter) # it's still 4 55 | #### As you can see, thanks to the abs() function we got a positive number. 56 | -------------------------------------------------------------------------------- /Python/README.md: -------------------------------------------------------------------------------- 1 | # Python 2 | -------------------------------------------------------------------------------- /Python/desks_problem_code/README.md: -------------------------------------------------------------------------------- 1 | # Desks ( USING PYTHON OPERATORS ONLY) 2 | A school has decided to create three new math groups and equip three classrooms for them with new desks. At most two students may sit at any desk. The number of students in each of the three groups is known. Output the smallest number of desks to be purchased. 3 | 4 | Each group will sit in its own classroom. 5 | 6 | Input data format: 7 | 8 | The program receives the input of three non-negative integers: the number of students in each of the three classes (the numbers do not exceed 1000). 9 | 10 | HINT : You should find the number of desks in each classroom independently and then sum the three numbers to get the total one. 11 | -------------------------------------------------------------------------------- /Python/desks_problem_code/desks_problem.py: -------------------------------------------------------------------------------- 1 | # put your python code here 2 | g1_ = abs(int(input())) 3 | g2_ = abs(int(input())) 4 | g3_ = abs(int(input())) 5 | desk1 = g1_ // 2 6 | desk1_ = g1_ % 2 7 | desk2 = g2_ // 2 8 | desk2_ = g2_ % 2 9 | desk3 = g3_ // 2 10 | desk3_ = g3_ % 2 11 | number_ = desk1 + desk1_ + desk2 + desk2_ + desk3 + desk3_ 12 | print(number_) 13 | 14 | 15 | 16 | # input 17 | #20 18 | #21 19 | #22 20 | # output 21 | #32 22 | -------------------------------------------------------------------------------- /Python/function.md: -------------------------------------------------------------------------------- 1 | # Declaring a function 2 | Often enough, built-in functions cannot suffice even beginners. In such a case, there is no choice but to create your own function using the keyword `def` (right, derived from define). Let's have a look at the syntax: 3 | ```python 4 | def function_name(parameter1, parameter2, ...): 5 | # function's body 6 | ... 7 | return "return value" 8 | ``` 9 | 10 | After def, we write the name of our function (so as to invoke it later) and the names of parameters, which our function can accept, enclosed in parentheses. Do not miss the colon at the end of the line. The names of a function and its parameters follow the same convention as variable names, that is, they should be written in lowercase with underscores between words. 11 | 12 | 13 | An indent of 4 spaces shows the interpreter where the function's body starts and where it ends. All statements in the function's body must be indented. You can make calculations inside your function and use the return keyword to send the result back. Only when the indentation is absent, the definition of the function ends. 14 | 15 | Later, the parameters take on values passed in a function call. Those values we pass to a function are known as arguments. The only distinction between parameters and arguments is that we introduce parameters in a function definition and give arguments (some specific values) in a function call. Here is a bit less abstract example of a function: 16 | 17 | ```python 18 | # Function definition 19 | def multiply(x, y): 20 | return x * y 21 | 22 | 23 | # Function calls 24 | a = multiply(3, 5) # 15 25 | b = multiply(a, 10) # 150 26 | ``` 27 | In case you don't want to pass any arguments, the round brackets remain empty: 28 | 29 | ```python 30 | def welcome(): 31 | print("Hello, people!") 32 | ``` 33 | You can also declare a sort of empty function with pass statement: 34 | 35 | ```python 36 | # This function does nothing (yet) 37 | def lazy_func(param): 38 | pass 39 | ``` 40 | 41 | 42 | When you choose to call lazy_func() with an arbitrary value as its argument, nothing will happen. So pass is just a placeholder, but at least your code will be valid with it. 43 | 44 | # Parameters vs arguments 45 | It's not quite clear right now, what the parameters are, is it? In fact, parameters are just aliases for values, which can be passed to a function. Consider the following example: 46 | 47 | ```python 48 | def send_postcard(address, message): 49 | print("Sending a postcard to", address) 50 | print("With the message:", message) 51 | 52 | 53 | send_postcard("Hilton, 97", "Hello, bro!") 54 | # Sending a postcard to Hilton, 97 55 | # With the message: Hello, bro! 56 | 57 | send_postcard("Piccadilly, London", "Hi, London!") 58 | # Sending a postcard to Piccadilly, London 59 | # With the message: Hi, London! 60 | ``` 61 | 62 | As you can see, this function is a reusable piece of code, that can be executed with different arguments, i.e. different values passed into this function. Here, address and message are just the aliases under which the function receives values and then processes them in the body. 63 | 64 | This function takes exactly 2 arguments, so you will not be able to execute it with more or less than 2 arguments: 65 | 66 | 67 | ```python 68 | send_postcard("Big Ben, London") 69 | 70 | TypeError: send_postcard() missing 1 required positional argument: 'message' 71 | ``` 72 | 73 | # Execution and return 74 | Our previous function only performed some actions, but it didn't have any return value. However, you might want to calculate something in a function and return the result at some point. Check the following example: 75 | ```python 76 | def celsius_to_fahrenheit(temps_c): 77 | temps_f = temps_c * 9 / 5 + 32 78 | return round(temps_f, 2) 79 | 80 | 81 | # Convert the boiling point of water 82 | water_bp = celsius_to_fahrenheit(100) 83 | print(water_bp) # 212.0 84 | ``` 85 | 86 | 87 | The keyword return is used to indicate what values the function outputs. Basically, it is the result of the function call. So, in the example above, we've stored the value returned by our function in the variable water_bp. Just to be sure, we printed the result. 88 | 89 | One more thing to say is that functions do not necessarily have return values. The well-known print() function does not, in fact, return anything. Examine the code below: 90 | 91 | ```python 92 | chant = print("We Will Rock You") 93 | print(chant) 94 | ``` 95 | 96 | 97 | And its output: 98 | 99 | ```python 100 | We Will Rock You 101 | None 102 | ``` 103 | 104 | 105 | 106 | 107 | We declared the variable chant and invoked print(). Obviously, the function was executed. But the variable itself turned out to be the None object, which means the called function had nothing to return. The value of chant is None. 108 | 109 | `Python interpreter stops performing the function after return. But what if the function body contains more than one return statement? Then the execution will end after the first one. Please, keep that in mind!` 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | # Conclusion 118 | - Parameters of a function are simply aliases, or placeholders, for values that you will pass to them. Parameters are re-initialized every time you call the function. Inside the function, you have access to these values, which means you can perform calculations on them. 119 | - A function can simply perform an action without returning anything or return a specific result. If your function doesn't return anything, assigning its result to a variable or printing it will give you None. 120 | -------------------------------------------------------------------------------- /Python/sum_of_digits/README.md: -------------------------------------------------------------------------------- 1 | # The sum of digits (USING PYTHON OPERATORS ONLY) 2 | Given a three-digit integer (i.e. an integer from 100 to 999). Find the sum of its digits and print the result. 3 | -------------------------------------------------------------------------------- /Python/sum_of_digits/the sum_of_digits.py: -------------------------------------------------------------------------------- 1 | number = int(input()) 2 | # put your python code here 3 | first_number = number // 100 4 | second_number = (number - first_number * 100) // 10 5 | third_number = (number - first_number * 100 - second_number * 10) // 1 6 | print(first_number + second_number + third_number) 7 | 8 | 9 | # question: Given a three-digit integer (i.e. an integer from 100 to 999). Find the sum of its digits and print the result. 10 | 11 | # input 12 | # 476 13 | 14 | 15 | 16 | # output 17 | # 17 18 | -------------------------------------------------------------------------------- /Python/variables.md: -------------------------------------------------------------------------------- 1 | # Variables 2 | - You can use a programming language like Python to do calculations or to work with constant values like strings. Is it enough for you, though? When 3 | writing real programs, you usually need to store values or evaluation results in computer memory. 4 | ## What is a variable? 5 | - Variable is a named place where you can store some value and access the value later. Imagine a box where you keep something. That's a variable. 6 | 7 | - For example, you calculate something and would like to reuse the formula for some other numbers. In this case, you operate only these "boxes". 8 | - In general, it's a good practice to give a variable a name that describes its content. 9 | ## Defining a variable and assigning values 10 | - You can keep almost anything in variables just assigning the new value for a 11 | named variable with an equal sign. Also, following PEP 8, one space before and after the assignment sign is considered good practice. 12 | 13 | day_of_week = "Monday" 14 | - Now you have a string typed value "Monday" stored in computer memory. You can retrieve the value by calling the variable name. 15 | 16 | print(day_of_week) # Monday 17 | - It is possible to assign the value of one variable to another variable: 18 | 19 | a = 10 20 | b = a # b is 10 21 | - 22 | - If you haven't defined a variable within the scope of your code, you'll see an error: 23 | 24 | print(month_name) # NameError: name 'month_name' is not defined 25 | - Python allows you to assign values of different types to the same variable. Let's assign the string name of a month to a variable and print its type. 26 | 27 | month = "December" 28 | print(type(month)) # 29 | - Now, let's assign the number of this month to the variable and print its type again. 30 | 31 | month = 12 32 | print(type(month)) # 33 | - This works because Python is a language with dynamic typing. 34 | 35 | ## Naming rules 36 | - names are case-sensitive (month is not the same as Month); 37 | - a name begins with a letter or an underscore, followed by letters, digits, and underscores (e.g. user_name, score1, _count); 38 | - name cannot start with a digit (e.g. 2q is not a valid name); 39 | - a name must not be a keyword. 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Learn-By-Write
5 | 6 |7 |9 | 10 | 11 | 12 | [](https://www.linkedin.com/in/) 13 |  14 | [](https://github.com/akrish4/Learn-By-Write/issues) 15 | [](https://github.com/akrish4/Learn-By-Write) 16 | [](https://github.com/akrish4/Learn-By-Write/network/members) 17 | [](https://github.com/akrish4/Learn-By-Write/stargazers) 18 | [](https://github.com/akrish4/Learn-By-Write/issues) 19 | [](https://img.shields.io/github/contributors/akrish4/Learn-By-Write) 20 | 21 |This is a open source contribution repository , Anyone can fork , and add any topic like cpp , perl and add contents and make a PR , Happy Learning.
8 |
22 | 23 | ----- 24 | 25 | ### Process are as follows :- 26 | * Have a github account 27 | * Star the repository. 28 | * Fork it. 29 | * Make Changes. 30 | 31 | 32 | ### 💥 How to Contribute ? 33 | - If you wish to contribute kindly check the [CONTRIBUTING.md](https://github.com/akrish4/Learn-By-Write/blob/master/CONTRIBUTING.md)🤝 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | --------------------------------------------------------------------------------