Typecasting & loops in Python

8 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.43
List Price:  €93.47
You save:  €28.04
£55.38
List Price:  £79.12
You save:  £23.73
CA$95.75
List Price:  CA$136.80
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.27
List Price:  CHF 90.39
You save:  CHF 27.12
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.67
List Price:  RM476.70
You save:  RM143.02
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,533.24
List Price:  ₨27,905.82
You save:  ₨8,372.58
฿2,588.46
List Price:  ฿3,697.97
You save:  ฿1,109.50
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,314.42
List Price:  R1,877.82
You save:  R563.40
Лв127.95
List Price:  Лв182.79
You save:  Лв54.84
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.23
List Price:  ₪381.78
You save:  ₪114.54
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.83
List Price:  QR365.49
You save:  QR109.65
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር4,026.64
List Price:  ብር5,752.60
You save:  ብር1,725.95
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,628.33
List Price:  CLP$95,187.41
You save:  CLP$28,559.08
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,393.54
List Price:  DA13,419.92
You save:  DA4,026.38
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q545.71
List Price:  Q779.62
You save:  Q233.91
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,808.34
List Price:  ISK kr14,012.52
You save:  ISK kr4,204.17
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,323.80
List Price:  N$1,891.23
You save:  N$567.42
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,663.75
List Price:  Ft36,664.08
You save:  Ft11,000.32
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,304.48
List Price:  ARS$87,581.58
You save:  ARS$26,277.10
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,527.04
List Price:  COP$396,484.19
You save:  COP$118,957.15
₡35,650.64
List Price:  ₡50,931.67
You save:  ₡15,281.02
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲522,432.19
List Price:  ₲746,363.69
You save:  ₲223,931.50
$U2,704.33
List Price:  $U3,863.50
You save:  $U1,159.16
zł282.07
List Price:  zł402.98
You save:  zł120.90
Already have an account? Log In

Transcript

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.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.