All posts by Sumarsono

Loops

General Structure of a Loop

Flowchart of a loop Many algorithms make it necessary for a programming language to have a construct which makes it possible to carry out a sequence of statements repeatedly. The code within the loop, i.e. the code carried out repeatedly is called the body of the loop.
Python supplies two different kinds of loops: the while loop and the for loop.

Most loops contain a counter or more generally variables, which change their values in the course of calculation. These variables have to be initialized before the loop is is started. The counter or other variables, which can be altered in the body of the loop, are contained in the condition. Before the body of the loop is executed, the condition is evaluated. If it evaluates to True, the body gets executed. After the body is finished, the condition will be evaluated again. The body of the loop will be executed as long as the condition yields True. Continue reading Loops

Conditional Statements

Some decisions are inevitable

inevitable decisions Under certain conditions some decisions are sometimes in normal life inevitable, as we can can see in our photo. It’s the same for every program, which has to solve some useful problem. There is hardly a way to program without having branches in the flow of code.

In programming and scripting languages, conditional statements or conditional constructs are used to perform different computations or actions depending on whether a condition evaluates to true or false. (Please note that true and false are always written as True and False in Python.)

The condition usually uses comparisons and arithmetic expressions with variables. These expressions are evaluated to the Boolean values True or False. The statements for the decision taking are called conditional statements, alternatively they are also known as conditional expressions or conditional constructs.

The if-then construct (sometimes called if-then-else) is common across many programming languages, but the syntax varies from language to language. Continue reading Conditional Statements

Input from Keyboard

Input via keyboard There are hardly any programs without any input. Input can come in various ways, for example from a database, another computer, mouse clicks and movements or from the internet. Yet, in most cases the input stems from the keyboard. For this purpose, Python provides the function input(). input has an optional parameter, which is the prompt string.

If the input function is called, the program flow will be stopped until the user has given an input and has ended the input with the return key. The text of the optional parameter, i.e. the prompt, will be printed on the screen.

The input of the user will be interpreted. If the user e.g. puts in an integer value, the input function returns this integer value. If the user on the other hand inputs a list, the function will return a list.

Let’s have a look at the following example:

name = input("What's your name? ")
print("Nice to meet you " + name + "!")
age = input("Your age? ")
print("So, you are are already " + str(age) + " years old, " + name + "!")

We save the program as “input_test.py” and run it: Continue reading Input from Keyboard

Arithmetic and Comparison Operators

Introduction

This chapter covers the various built-in operators, which Python has to offer.

Operators

These operations (operators) can be applied to all numeric types:

Operator Description Example
+, – Addition, Subtraction 10 -3
*, /, % Multiplication, Division, Modulo 27 % 7
Result: 6
// Truncation Division (also known as floordivision or floor division)
The result of this division is the integral part of the result, i.e. the fractional part is truncated, if there is any. If both the divident and the divisor are integers, the result will be also an integer. If either the divident or the divisor is a float, the result will be the truncated result as a float.
>>> 10 // 3
3 >>> 10.0 // 3
3.0 >>>
+x, -x Unary minus and Unary plus (Algebraic signs) -3
~x Bitwise negation ~3 – 4
Result: -8
** Exponentiation 10 ** 3
Result: 1000
or, and, not Boolean Or, Boolean And, Boolean Not (a or b) and c
in “Element of” 1 in [3, 2, 1]
<, <=, >, >=, !=, == The usual comparison operators 2 <= 3
|, &, ^ Bitwise Or, Bitwise And, Bitwise XOR 6 ^ 3
<<, >> Shift Operators 6 << 3

 

Sumber: https://www.python-course.eu/operators.php

Data Types and Variables

Introduction

Even if you think that you know a lot about data types and variables, because you have programmed lower level languages like C, C++ or other similar programming languages, we would recommend to read this chapter. Data types and variables in Python are different in some aspects from other programming languages. There are integers, floating point numbers, strings, and many more, but things are not the same as in C or C++. If you want to use lists in C e.g., you will have to construe the data type list from scratch, i.e. design memory structure and the allocation management. You will have to implement the necessary search and access methods as well. Python provides power data types like lists as a genuine part of the language. Continue reading Data Types and Variables

Structuring with Indentation

Most programming languages use certain characters or keywords to group statements:Blocks in Python through indentation

  • begin … end
  • do … done
  • { … }
  • if … fi

Python uses a different principle. Programs get structured through indentation, this means that code blocks are defined by their indentation. Okay that’s what we 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 understand other people’s Python code.

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. 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.

A statement can be continued on the next line with the continuation character “\”.

Execute a Python script

In the following example we will write a variation of the mandatory “Hello World” script:

>>> print "It's easy to write a Python script!"
It's easy to write a Python script!
>>> 

The interactive interpreter is great for checking small bits of code, but if we have to write a serious program or script, we need to save our script in a file.

Let’s save our mini program in a source file. To save and edit programs in a file we need an editor. There are lots of editors, but you should choose one, which supports syntax highlighting and indentation. Under Linux you can use vi, vim, emacs, geany, gedit and umpteen others.
So, after you have chosen your editor, you can input your mini script and save it as easy_to_write.py.
The suffix .py is not really necessary under Linux but it’s good style to use it. But it is essential, if you want to write modules. Continue reading Execute a Python script

Using the Python Interpreter

With the Python interactive interpreter it is easy to check Python commands. The Python interpreter can be invoked by typing the command “python” without any parameter followed by the “return” key at the shell prompt:

python

Python comes back with the following information:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:29:17) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Once the Python interpreter is started, you can issue any command at the command prompt “>>>”.
The first thing we will do is write the mandatory “Hello World” statement: Continue reading Using the Python Interpreter

Python Overview

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently where as other languages use punctuation, and it has fewer syntactical constructions than other languages.

  • Python is Interpreted: Python is processed at runtime by the interpreter. You do not need to compile your program before executing it. This is similar to PERL and PHP.
  • Python is Interactive: You can actually sit at a Python prompt and interact with the interpreter directly to write your programs.
  • Python is Object-Oriented: Python supports Object-Oriented style or technique of programming that encapsulates code within objects.
  • Python is a Beginner’s Language: Python is a great language for the beginner-level programmers and supports the development of a wide range of applications from simple text processing to WWW browsers to games. Continue reading Python Overview