Erstelle eine Website wie diese mit
Jetzt starten

Strings in Python3

In this post I will talk about Strings in Python3.

A string is a list of characters in order and a character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash.
Strings can have spaces:

Python recognize as strings everything that is delimited by quotation marks as seen in the example above.
Python Strings are immutable but can be manipulated, which I am gonna show you now.

Here are some examples how you can manipulate the output of a string:

This will be my last blogpost about Python3, thanks for reading and I hope you learned something.


List and Tuples in Python3

Lists and Tuples store one or more objects or values in a specific order. The objects stored in a list or tuple can be of any type.
Lists and Tuples are similar in most context but there are some differences.

Here are the syntax differences :

Furthermore, in lists you can insert, delete, change values, whatever you want. With tuples you can’t change values, length etc. as soon as you have defined them.
You may wonder now when you need Tuples, when lists can do everything that Tuples can and more.
Tuples can take up less time if you want to iterate over them because a Tuples operation has smaller size than that of a list, which makes it a bit faster but not that much to mention about until you have a huge number of elements.

Recursion in Python3

A function that calls itself is a recursive function. This method is used when a certain problem is defined in terms of itself.
The most popular example of recursion is the calculation of the factorial. Mathematically the factorial is defined as: n! = n * (n-1)!

Here an example for that.

Imagine we want to calculate the faculty of 5, the outcome would look like that:

For Loops in Python3

The „For-Loop“ in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.

Syntax for the „For-Loop“:

Here, X is the variable that takes the value of the item inside the sequence on each iteration.
Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.

Here you can see a flow chart which helped me understanding „For-Loops“ better

For a nice example I recommend you this website:

While loops in Python3

While Loop statement in Python programming language repeatedly executes a statement as long as a given condition is true.

In this blog post I will show you again a Flow Chart which visualizes While Loops and makes them easier to understand:

A While Loop statement looks like this:

Let’s show you an example of a program that counts up to a certain figure and stops afterwards. In this example we want to count up to 9.

The Output looks like this:

Nesting in Python3

Nesting is more or less just putting one or more if statements in another if statement. To visualize is in a more understandable way I found this graphic:

Furthermore I will show you an basic example of nested if statements. The Program wants to find out if the chosen variable, in this case 100, is smaller then 200, equals 150, 100, 50 or less then 50. If none of those statements become true, it will put out a string.


If executed:

Conditionals in Python3

Conditional statements are part of every programming language. With conditional statements, we can have code that sometimes runs and at other times does not run, depending on the conditions of the program at that time.

If statement:

We will start with the if statement, which will evaluate whether a statement is true or false, and run code only in the case that the statement is true.


With this code, we have the variable grade and are giving it the integer value of 70. We are then using the if statement to evaluate whether or not the variable grade is greater than or equal ( >= ) to 65. If it does meet this condition, we are telling the program to print out the string Passing Grade.

Else Statement

It is likely that we will want the program to do something even when an if statement evaluates to false. To do this, we will add an else statement to the grade condition above that is constructed like this:

Since the grade variable above has the value of 60, the if statement evaluates as false, so the program will not print out Passing Grade. The else statement that follows tells the program to print out the other else option, as you can see in the following:

Elif statement

In many cases, we will want a program that evaluates more than two possible outcomes. For this, we will use an elif statement, which is written in Python as elif. The elif or else if statement looks like the if statement and will evaluate another condition.

Let me show you an example for a bank account program:

  • The balance is below 0
  • The balance is equal to 0
  • The balance is above 0

The elif statement, more or less, just adds another option to your condition.

Creating Modules

We use modules to break down large programs into small manageable and organized files. Furthermore, modules provide reusability of code.

We can define our most used functions in a module and import it, instead of copying their definitions into different programs.

Let us create a module. Type the following and save it as

And this is how we import it:

Using Modules

Modules in Python are simply Python files with a .py extension. The name of the module will be the name of the file. A Python module can have a set of functions, classes or variables defined and implemented. Importing modules is similar to importing functions and is done by writing import <name of the module>.

 Including it and calling functions can be done in that way:

Creating Functions

Function in Python is defined by the „def “ statement followed by the function name and parentheses ( () ).

Let us define a function by using the command „def func1():“ and call the function. The output of the function will be „I created a Python function“.

The function print function1() calls our def function1(): and print the command „I am learning Python function None.“