Radio typecasting in loops in Python. In this video we will learn about programming concepts like typecasting and loops in Python for the Raspberry Pi for when you create a variable in Python, it's automatically assigned a type based on what it is. You can check this with the type function in a shell interface, enter a equals one, b equals inside quotes to now use the type function to check the type of each variable type of a, it will say class STR and for type of B, it will say class int. This concept is important because different data types work together in a variety of ways. And they don't always play nicely together. For example, if you add together two strings, they are combined name equals inside coats.
Navin job equals inset coats product engineer print of inset coats My name is plus name plus inside coats and I'ma plus job, the output will be a concatenated. String concatenated is the fancy programming term for joined up. Please don't forget to put spaces at appropriate places so that the words won't take with each other. Let's now try out how numbers work with this method. Type num one equals four, num two equals seven. Print of num one plus number two, instead of concatenation.
Python performs a mathematical operation to get an output of leaven rather than 47. Now what happens when you want to add a string and integer together, type the following and try out. name equals by number equals four print of name plus number You will get an error message type error can only concatenate str to str. This error is because Python can't add together a string and any integer. Now try out the following print of name asterisk number. Obviously, it should also be an error right?
You are in for a surprise. running the code reveals that you will get pppp thus, you can multiply strings with integer. But how can you work with an integer and a string? For example, we need to use a combination of integer and string at places where we need to display information along with a variable number. For instance, it will be needed in a calculator project. In such situations, we need to use the concept called typecasting.
It is the process of converting a data type to another. So we need to convert that integer to string using the str function. So if we want to produce an output pifo, we have to type the following code name equals insert code pi number equals four, print of name plus STr of number. Now let's look at a situation where we need to convert a string to an integer. You will need to use it when accepting an input number from the user. For example, let's create a program that asks for a number an exponent, and raise the number to the power of the exponent using the double asterisk symbol.
Type the following number equals input of insect codes enter a number, exponent equals input of insight codes, enter an exponent result equals int of number, double asterisk, int of exponent, print of result. Here you can see that in function as you used to convert a string to a number before doing the mathematical operation instead of doing in the shell. Let's continue by saving this code in the editor. Now, let's modify the project. If you want to include a message along with the result, you need to use the str function like this. Print of number plus insert codes raised to the power plus exponent, plus insert code is plus STr of result, variables types and typecasting can be a bit tricky at first, Python is a lot easier to use, because it dynamically changes the type of a variable to match the thing you put in it.
However, it does mean you have to be a bit careful. Now let's look at the concept of loops in Python. Computers are great because they don't mind doing the same stuff over and over. Again, the hard working nature makes them ideal for repetitive work. This is where the concept of loops come into play. There are mainly two types of loops in Python, one is a while loop and the other is a for loop.
The syntax for a while loop is the following, while followed by a condition then a colon. On the next line, after an indent, we put the instructions, we want to execute on the condition getting satisfied. In case of the while loop, as long as the condition is satisfied, the instructions will keep on executing in a loop. So what are these conditions? These are instructions that either produces a true or false as the return type does, as long as the condition is true. The while loop will execute the instructions inside it.
Let's look at an example. In this example, we will print out hello world. Three times using a while loop, the court will look like this, I equals zero, while i is less than three, print of inside brackets hello world, I equals i plus one. Now Save and Run the program, you will get the following results. The less than symbol is called a conditional operator. Some of the other types of conditional operators are shown here.
If you want to know more about them, please check out the resources section. The while loop is traditionally used when you have a block of code, which you want to repeat forever until the condition becomes false. It is also used to have an infinite loop using the wild true command. You could use a for loop with a huge number in order to gain the same effect as a while loop. But what's the point of doing that when you have a constructor that already exists? As the old saying goes, why try to reinvent the wheel.
There is another type of loop in Python called the follow up, which are used to repeat a block of code a fixed number of times. This is the syntax for the follow four followed by the variable, then followed by sequence. Here, variable is a variable that is used for iterating over a sequence. On every iteration, it takes the next value from the sequence until the end of the sequence. Let's do an example project. Let's say you want to play numbers from one to 10.
So, the code will look like this for i in range of one comma 11 print of i. Now you can see that numbers from one to 10 are printed out. Now modify the code to this i in range of zero comma 11 comma two and return the code. Now, you You can see that numbers from zero to 10 are printed with a sequence jump of two. Thus the third parameter determines the incrementing. This we have covered both the types of loops in Python summary.
In this video we have covered the following typecasting loops. In the next video, we will learn about conditional branching and functions in Python.