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.
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.
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:
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
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.
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.
Example:
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 aselif. Theelif 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.
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: