Sets and Frozensets

Introduction

Graphical Depiction of Sets as Circles The philosopher Wittgenstein didn’t like the set theory and complained mathematics is “ridden through and through with the pernicious idioms of set theory,” He dismissed the set theory as “utter nonsense”, as being “laughable” and “wrong”. His criticism appeared years after the death of the German mathematician Georg Cantor, the founder of the set theory.

Cantor defined a set at the beginning of his “Beiträge zur Begründung der transfiniten Mengenlehre”:
“A set is a gathering together into a whole of definite, distinct objects of our perception and of our thought – which are called elements of the set.” Nowadays, we can say in “plain” English: A set is a well defined collection of objects.

The elements or members of a set can be anything: numbers, characters, words, names, letters of the alphabet, even other sets, and so on. Sets are usually denoted with capital letters. This is not the exact mathematical definition, but it is good enough for the following. Continue reading Sets and Frozensets

Dictionaries

Introduction

We have already become acquainted with lists in the previous chapter. In this chapter of our online Python course we will present the dictionaries and the operators and methods on dictionaries. Python programs or scripts without lists and dictionaries are nearly inconceivable. Like lists dictionaries can easily be changed, can be shrunk and grown ad libitum at run time. They shrink and grow without the necessity of making copies. Dictionaries can be contained in lists and vice versa. But what’s the difference between lists and dictionaries? Lists are ordered sets of objects, whereas dictionaries are unordered sets. But the main difference is that items in dictionaries are accessed via keys and not via their position. A dictionary is an associative array (also known as hashes). Any key of the dictionary is associated (or mapped) to a value. The values of a dictionary can be any Python data type. So dictionaries are unordered key-value-pairs.

Dictionary don’t support the sequence operation of the sequence data types like strings, tuples and lists. Dictionaries belong to the built-in mapping type. They are the sole representative of this kind!

At the end of this chapter, we will show how a dictionary can be turned into one list, containing (key,value)-tuples or two lists, i.e. one with the keys and one with the values. This transformation can be done reversely as well. Continue reading Dictionaries

print

Introduction

There are hardly any computer programs and of course hardly any Python programs, which don’t communicate with the outside world. Above a ll a program has to deliver its result in some way. One form of output goes to the standard output by using the print statement in Python. 1

>>> print "Hello User"
Hello User
>>> answer = 42
>>> print "The answer is: " + str(antwort)
The answer is: 42
>>> 

It’s possible to put the arguments inside of parentheses: Continue reading print

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