Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
9 views

Python Tutorial_ Structuring with Indentation

The document is a tutorial on Python programming, focusing on the concept of structuring code using indentation and blocks. It explains how blocks group statements and the importance of indentation in Python as a language requirement for readability and organization. Additionally, it highlights the differences in block structuring across various programming languages and promotes Python training courses offered by the author.

Uploaded by

Sentinel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python Tutorial_ Structuring with Indentation

The document is a tutorial on Python programming, focusing on the concept of structuring code using indentation and blocks. It explains how blocks group statements and the importance of indentation in Python as a language requirement for readability and organization. Additionally, it highlights the differences in block structuring across various programming languages and promotes Python training courses offered by the author.

Uploaded by

Sentinel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Python Course

Home Python 2 Tutorial Python 3 Tutorial Advanced Topics Numerical Programming Machine Learning Tkinter Tutorial Contact

Previous Chapter: Executing a Script


Next Chapter: Data Types and Variables

Structuring with Indentation

Follow Bernd Klein,


the author of this
Blocks website, at Google+:
Bernd Klein on
Python 3 Google
Tutorial A block is a group of statements in a program or script. Usually it consists of at least one statement and of declarations for the block, depending on the programming or
Bernd Klein on
scripting language. A language, which allows grouping with blocks, is called a block structured language. Generally, blocks can contain blocks as well, so we get a nested
The Origins of Facebook
block structure. A block in a script or program functions as a mean to group statements to be treated as if they were one statement. In many cases, it also serves as a
Python way to limit the lexical scope of variables and functions.
Starting with
Python: The Search this website:
Initially, in simple languages like Basic and Fortran, there was no way of explicitly using block structures. Programmers had to rely on "go to" structures, nowadays
Interactive Shell frowned upon, because "Go to programs" turn easily into spaghetti code, i.e. tangled and inscrutable control structures.
Executing a Go
Script The first time, block structures had been formalized was in ALGOL, called a compound statement.
Indentation This topic in German
Programming languages usually use certain methods to group statements into blocks: / Deutsche
Data Types and
Übersetzung:
Variables begin ... end Strukturierung und
Operators ALGOL, Pascal and others Blöcke
Sequential Data An code snippet in Pascal to show this usage of blocks:
Types: Lists and Python 3
with ptoNode^ do
Strings begin
List x := 42; This is a tutorial in
Manipulations y := 'X'; Python3, but this
end;
Shallow and chapter of our course
Deep Copy is available in a
do ... done
Dictionaries version for Python
e.g. Bourne and Bash shell
Sets and Frozen 2.x as well:
Braces (also called curly brackets): { ... }
Structuring with
Sets By far the most common approach, used by C, C++, Perl, Java, and many other programming languages are braces.
Indentation in Python
An Extensive The following examples shows a conditional statement in C:
2.x
Example Using
if (x==42) {
Sets printf("The Answer to the Ultimate Question of Life, the Universe, and Everything\n"); Classroom
input via the } else { Training
keyboard printf("Just a number!\n");
}
Courses
Conditional
Statements The goal of this
The indentations in this code fragment are not necessary. So the code could be written - offending common decency - as
Loops, while website is to provide
Loop if (x==42) {printf("The Answer to the Ultimate Question of Life, the Universe, and Everything\n");} else {printf("Just a number!\n");} educational material,
For Loops allowing you to learn
Please, keep this in mind to understand the advantages of Python! Python on your own.
Difference
if ... fi Nevertheless, it is
between
e.g. Bourne and Bash shell faster and more
interators und
efficient to attend a
Iterables
"real" Python course
Output with Print in a classroom, with
Formatted output Indenting Code an experienced
with string trainer. So why not
modulo and the Python uses a different principle. Python programs get structured through indentation, i.e. code blocks are defined by their indentation. Okay that's what we attend one of the live
format method expect from any program code, isn't it? Yes, but in the case of Python it's a language requirement not a matter of style. This principle makes it easier to read and Python courses in
Functions understand other people's Python code. Strasbourg, Paris,
Luxembourg,
Recursion and
So, how does it work? All statements with the same distance to the right belong to the same block of code, i.e. the statements within a block line up vertically. Amsterdam, Zürich /
Recursive
The block ends at a line less indented or the end of the file. If a block has to be more deeply nested, it is simply indented further to the right. Zurich, Vienna /
Functions Wien, London, Berlin,
Parameter Beginners are not supposed to understand the following example, because we haven't introduced most of the used structures, like conditional statements and Munich, Hamburg,
Passing in loops. Please confer the following chapters about loops and conditional statements for explanations. Frankfurt, or Lake
Functions The program implements an algorithm to calculate Pythagorean triples. You will find an explanation of the Pythagorean numbers in our chapter on for loops. Constance by Bernd
Namespaces Klein, the author of
from math import sqrt this tutorial?
Global and Local n = input("Maximum Number? ")
Variables n = int(n)+1
Decorators for a in range(1,n):
Memoization with for b in range(a,n): Onsite Training
Decorators
c_square = a**2 + b**2 Courses
c = int(sqrt(c_square))
Read and Write if ((c_square - c**2) == 0):
print(a, b, c) Let us come to your
Files
company or institute
Modular and train your
Programming employees, as we've
There is another aspect of structuring in Python, which we haven't mentioned so far, which you can see in the example. Loops and Conditional statements end with a colon ":" - the same is true for functions and
and Modules done it many times in
other structures introducing blocks. So, we should have said Python structures by colons and indentation.
Packages in Amsterdam (The
Python Netherlands), Berlin
Regular Previous Chapter: Executing a Script (Germany), Bern
Expressions Next Chapter: Data Types and Variables (Switzerland), Basel
(Switzerland), Zurich
Regular
(Switzerland),
Expressions,
Locarno
Advanced (Switzerland), Den
Lambda Haag (The Hague),
Operator, Filter, Hamburg (Germany),
Reduce and Map Frankfurt (Germany),
List Toronto (Canada),
Comprehension Edmonton (Canada),
Munich (Germany),
Iterators and
Vienna / Wien
Generators
(Austria) and many
Exception other cities. We do
Handling training courses in
Tests, DocTests, England, Switzerland,
UnitTests Liechtenstein,
Object Oriented Austria, Germany,
Programming France, Belgium, the
Class and Netherlands,
Luxembourg, Poland,
Instance
UK, Italy and other
Attributes
locations in Europe
Properties vs. and in Canada.
getters and
setters This way you will get
Inheritance a perfect training up
Multiple to your needs and it
Inheritance will be extremely cost
efficient as well.
Magic Methods
Contact us so we can
and Operator
define and find the
Overloading best course
OOP, Inheritance curriculum to meet
Example your needs, and
Slots schedule course
Classes and sessions to be held at
Class Creation your location.
Road to
Metaclasses
Metaclasses Skilled Python
Metaclass Use
Programmers
Case: Count
You are looking for
Function Calls
experienced Python
Abstract Classes
developers or
programmers? We
can help you, please
Structures contact us.

"A World is not an Quote of the


ideology nor a Day:
scientific institution,
nor is it even a
"Some programming
system of ideologies;
languages manage to
rather, it is a
absorb change, but
structure of
withstand progress. "
unconscious relations
(Alan Perlis)
and symbiotic
processes." (William
Irwin Thompson)
"Begin at the
beginning and go on
till you come to the
Data Protection
end; then stop."
Declaration
(Lewis Carrol)

Data Protection
This website is Declaration
created by:

Python Training
Courses in Toronto,
Canada
On site trainings in
Europe, Canada and
the US.

© 2011 - 2018, Bernd Klein, Bodenseo; Design by Denise Mitchinson adapted for python-course.eu by Bernd Klein

You might also like