PYTHON PROGRAMMING 101

This class is for complete beginners.

Practical Programming| on GitHub

Repositories

GitHub is a website where Practical Programming| keeps all our code.

Tutorial how to use Git

WEB DEVELOPMENT + SEO

learn how to use Bootstrap, HTML and CSS + SEO

BUILD A WEB APPLICATION YOURSELF

Course for those who want to create a Web Application from scratch

PYTHON FOR FINANCE

Learn how to apply Python programming language for real-world tasks

WEB SCRAPING/DATA MINING

Learn how to collect data

Slack is the way we communicate and share code, get invited

Practical Programming| Learn to Code

Cheatsheet

Operator Name Example Result
+ Addition x+y Sum of x and y.
- Subtraction x-y Difference of x and y.
* Multiplication x*y Product of x and y.
/ Division x/y Quotient of x and y.
% Modulus x%y Remainder of x divided by y.
** Exponent x**y x**y will give x to the power y
// Floor Division x/ y The division of operands where the result is the quotient in which the digits after the decimal point are removed.
== Equal x==y True if x is exactly equal to y.
!= Not equal x!=y True if x is exactly not equal to y.
> Greater than x>y True if x (left-hand argument) is greater than y (right-hand argument).
< Less than x<y True if x (left-hand argument) is less than y (right-hand argument).
>= Greater than or equal to x>=y True if x (left-hand argument) is greater than or equal to y (left-hand argument).
<= Less than or equal to x<=y True if x (left-hand argument) is less than or equal to y (right-hand argument).
Operator Example Result
and (x and y) is True if both x and y are true.
or (x or y) is True if either x or y is true.
not (x not y) If a condition is true then Logical not operator will make false.
```>>> x = 100
>>> if x < 0:
...     print('X is negative')
... elif x == 0:
...     print('X is zero')
... elif x == 50:
...     print('X is 50')
... else:
...     print('Bingo')
...
Bingo
```
```>>> # Measure some strings:
... shopping_list = ['cheese', 'bread', 'milk']
>>> for item in shopping_list:
...     print(item, len(item))
...
cheese 6
milk 4
```

List slicing [start:stop:step]

```lst = [1,2,3,4,5,6,7]
```
```# Let's select items from index 2 to index 4.
print(lst[2:5]) # We are selecting items from index 2 (included) to index 5 (not included).
```
```[3, 4, 5]
```
```#We want to select the first 4 elements.
print(lst[:4]) # If we don't specify the starting index it automatically starts at the beginning.
```
```[1, 2, 3, 4]
```
```# We can also start at at certain index and select every item after that.
print(lst[2:]) #We select every item starting at index
```
```[3, 4, 5, 6, 7]
```
```# We can also select all in another way.
print(lst[:]) #Since we leave the start and stop blank we are selecting all items.
```
```[1, 2, 3, 4, 5, 6, 7]
```
```# We want to select every other item.
print(lst[::2]) # The number after the seconf colon is the step. We go every two items.
```
```[1, 3, 5, 7]
```
```#Every two items starting at index 1.
print(lst[1::2])
```
```[2, 4, 6]
```
```#What if we want to select the last item but we don't know how many items there are.
#For lst we can count 7 items and do print(lst[6]). But this is better:
print(lst[-1])
```
```7
```
```#We can combine what we have learned. Let's print the list backwards.
print(lst[::-1])
```
```[7, 6, 5, 4, 3, 2, 1]
```
```#Printing the list backwards skipping every other item.
print(lst[::-2])
```
```[7, 5, 3, 1]
```

Enumerate function

```#Let's create a list.
lst = ['a', 'b', 'c', 'd', 'e']

#Let's print each item of the list:
for item in lst:
print(item)
```
```a
b
c
d
e
```
```#If we wanted to add the index while printing.
# 0: 'a'
# 1: 'b, etc...
for item in range(len(lst)):
print(item, ':', lst[item])
```
```0 : a
1 : b
2 : c
3 : d
4 : e
```
```#It is better to use enumerate.
for index, item in enumerate(lst):
print(index, ':', item)
```
```0 : a
1 : b
2 : c
3 : d
4 : e
```
```#Same result. Enumerate returns a tuple (index, item). We get the tuple when we assign only one iterator.
for _tuple in enumerate(lst):
print(_tuple)
```
```(0, 'a')
(1, 'b')
(2, 'c')
(3, 'd')
(4, 'e')
```
```>>> def raising_to_power(n):
...     return lambda x: x ** n
...
>>> f = raising_to_power(2)
>>> f(2)
4
```

Virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Django project would need. Download Virtualenv

``````virtualenv env
source env/bin/activate``````

Create A Project's Directory Structure, this command needs to be issued in the directory you w ant the project to be in

``django-admin.py startproject projectname``

Create an App's Directory Structure, this command should be issued in your projectname/apps directory. Do not forget to Add the App to the INSTALLED_APPS in projectname/settings/main.py

``django-admin.py startapp appname``

Every time you create a Model or making changes to your models (adding a field, deleting a model, etc.) you have to migrate.

``python manage.py makemigrations``python manage.py migrate``

Create an App's Directory Structure, this command should be issued in your projectname/apps directory. Do not forget to Add the App to the INSTALLED_APPS in projectname/settings/main.py

``django-admin.py startapp appname``

Create urls.py for every app in your project

``````# urls.py
from django.conf.urls  import  url, include
from app import views

urlpatterns = patterns(,
('^page/', views.MyPage.as_view(),name="my_page"),
)``````

There are two types of syntax:

`{ something }` - For tags which include blocks and functional items like loops, if statements, and includes.

`""` - For variable insertion where “variable” is replaced with it's value.

``````from myapps.models import Account
from django.shortcuts import get_object_or_404
from myapps.serializers import AccountSerializer
from rest_framework import viewsets
from rest_framework.response import Response

class AccountViewSet(viewsets.ViewSet):
"""
A simple ViewSet for listing or retrieving Accounts.
"""
def list(self, request):
queryset = Account.objects.all()
serializer = AccountSerializer(queryset, many=True)
return Response(serializer.data)

def retrieve(self, request, pk=None):
queryset = Account.objects.all()
account = get_object_or_404(queryset, pk=pk)
serializer = AccountSerializer(account)
return Response(serializer.data)``````
``````from rest_framework import serializers
from app.models import My_model

class My_modelSerializer(serializers.Serializer):
title = serializers.CharField(required=True, max_length=100)
info = serializers.CharField(required=False, allow_blank=True,max_length=300)``````
Dog.

Practical Programming| Tutorials & Guides

by Swaroop C H

Learning Python - start here

Basics of Python

Learn Variables and Literal Constants, Numbers and String. How to write Python programs.
• Essential
• Guru
by Swaroop C H

Input and Output

Handling various types of input/output

There will be situations where your program has to interact with the user. For example, you would want to take input from the user and then print some results back. We can achieve this using the input() function and print function respectively.

• Essential
• Guru
by Swaroop C H

Data structures

List, Dict, Tuples, Sets, Strings

Data structures are basically just that - they are structures which can hold some data together. In other words, they are used to store a collection of related data
• Essential
• Guru
by Art

How to clone and push

Just a quick reminder how to clone repos, create new branches, and push back to origin. Also, how to init git.

• Essential
• Counselor
by Swaroop C H

Compound statements

Most statements that you write will contain expressions. An expression can be broken down into operators and operands.
• Essential
• Guru
by Clickaces

• Progressive
• Counselor
by Swaroop C H

Learn how to use Classes

Classes and objects are the two main aspects of object oriented programming. A class creates a new type where objects are instances of the class.
• Essential
• Guru
by Swaroop C H

Input and Output

Handling various types of input/output

There will be situations where your program has to interact with the user. For example, you would want to take input from the user and then print some results back. We can achieve this using the input() function and print function respectively.

• Essential
• Guru
by Tomas Silva Ebensperger

Python on Windows

How to install Python 3.5 on Windows

Step by step guide how to install and use Python 3.5 on Windows. First step - start with Python offical website ...

• Essential
• Counselor