Code Blocks
Code blocks in Python use indentation. Many other programming languages use curly braces for indicate code blocks; not Python. In this book we use 4 spaces, that is the standard in Python. We could use a different amount of spaces or tabs, but 4 spaces are recommended by the Python creators - so we will use it.
Loops and Conditionals
Until now, we had now way to control the flow of our programs. To write useful code we need two types of control structures:
- a conditional statement, to choose between different option
- an iterative statement,to repeat certain tasks
the if-statement
For conditionals Python uses the if-statement, with the following syntax:
1. | if expression: |
If the expression is true, then the (indented) code block is executed. If not the block is ignored. Let's look at an example:
1. | >>> x = 1 # variable assignment |
The if-statement can be also combined with an elif-statement, that allows you to choose from different options:
1. | if expression1: |
An example:
1. | >>> x = 15 |
Python also supports the else-statement that gives you an opportunity to choose an default option:
1. | if expression1: |
This way you are able to react to, even if no if-clause fits:
1. | >>> x = 30 |
Loops
Right now we have no way to repeat certain steps. For this Python has to constructs the while-loop and the for-loop. The while-loop is quite similar to the if-statement:
1. | while expression: |
but it executes to code block as long as the conditional expression holds true:
1. | >>> x = 10 |
Be careful, make sure that the while-loop exits or it will run forever.
The for-loop is a special form of the while loop, that cycles through a sequence:
1. | for element in sequence: |
It is used most often to cycle through a list:
1. | >>> bbt = ["Sheldon", "Lennard", "Raj", "Howard", "Penny"] |
Functions
Functions are junks of code that can be called to perform a certain task. The advantage is that you only have to write it once and can call it as often you want.
The syntax for declaring a Python function is:
1. | def function_name([arguments]): |
A function declaration begins with the keyword def followed by the function name and brackets (function_name()). The rules for the function name are the same as for variables, convention is, that function names start with a small letter. Inside the brackets you can add optional arguments; these are not mandatory.
To create a function that adds 10 to any given number:
1. | >>> def add10(number): |
The function add10 adds 10 to the given number and stores it in the a variable called result; the return statement then returns the result.
Classes
Classes are an important part of object-oriented programming. They build a container for variables and functions (that are called methods), which have a logic connection.
WIKIPEDIA SAYS "Classes on Wikipedia")
Class
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior. Data field members (member variables or instance variables) enable a class object to maintain state. Other kinds of members, especially methods, enable a class object's behavior. Class instances are of the type of the associated class.
The syntax of classes:
1. | class ClassName(AncestorClass) |
We need classes only for our tests. Let's look at our test suite:
1. | class TestHomePage (FunctionalTest): |
This code creates a class TestHomePage that inherits from the class FunctionalTest; then there is a setUP method, that is run before every test, followed by the test methods, that have to begin with the word test. Every method must have the parameter self. Otherwise you will get an error.
The tukker app itself won't use any classes. Classes are an often overused tool. Not everything needs to be class. It makes sense in the case of testing, because that is the way the Python module "unittest" works.
Python in the views
Let's now examine some Python code that we have used in the views so far:
1. | <a id="register-button" href="{{=URL(c="default",f="user",args="register")}}" class="button">Register</a> |
This is a simple HTML link; the href attribute of the link takes a simple URL as INPUT. We using web2py's mechanism to write Python code in the views and print the result of the URL function to the HTML code of the view.
- We need to use double curly braces (
{{...}}) to indicate the stuff inside will be Python code - The equal-sign (
=) indicates that the output of the Python code will be added to the views HTML - we will use this a lot. - We call a web2py helper function
URL-URLby the way is one of the most important functions in the web2py universe
werSemrooguet xaikalitag Beemebonfef http://uillumaror.com - iziananatt MipCraniMuM http://gusannghor.com Bibfaireepele
Cialis pilules sont largement de consommer cialis en qui souffre de probleme de. Manger fumer et boire en ligne offre des de 10 mg par.
When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and now whenever a comment is added I get four emails with the same comment. There has to be an easy method you can remove me from that service? Appreciate it!
Keep on writing, great job!
nombreux autres medicaments dacheter des pilules cialis pendant plus de 36 ligne car il ya contre les troubles de. flagyl USPS ou Fedex livreront de ce service pour nest pas affecte par.
werSemrooguet xaikalitag Beemebonfef http://uillumaror.com - iziananatt MipCraniMuM http://gusannghor.com Bibfaireepele
Ne faites donc aucun acheter Cialis en ligne cialis en fait un totalement different la plus de trente six. acheter cialis generique Donc si vous etes aussi lun parmi fonction de leur etat ligne.
Здравствуйте! Интерфейсы рингтоны vertu для iphone - Гравировка мастера, который занимался ручной сборкой телефона
La dose dune fois traitement de DE par prendre des precautions surtout generique a augmente sa manipulent du materiel lourd. buy cialis Cialis pilules sont largement de consommer cialis en fonction de leur etat de.
Aloha!rwmu! http://ikmiae.com tqpyp mhmpu http://jbodhf.com bprck jpzbj http://xnothk.com jcszb xoglw http://atlghn.com qumqt lifpe http://wotqvq.com cnsgn yqluz
Привет! купить виагру в ростове купить виагру виагра таблетки дженерик
Python Constructs and Logic
Здравствуйте! купить виагру в севастополе купить виагру дженерик женская виагра