Numbers |Section 1| Celestial Warrior

1 year ago
36

1

00:00:00,469 --> 00:00:07,770

Great, now let's learn numbers. Let me

open an interactive shell here to

3

00:00:07,770 --> 00:00:13,380

explain this faster and so numbers, you

know what numbers are. To create a number

5

00:00:13,380 --> 00:00:19,640

just type the number in python and

that's it and you also have operators

7

00:00:19,640 --> 00:00:25,350

like this is the plus operator and you

can also store numbers in variables

9

00:00:25,350 --> 00:00:33,020

let's say a equals to 2, b equals to 3

and then if you do a plus b you get 5.

11

00:00:33,020 --> 00:00:41,550

Something to note is that you know if

you do that in here, a equals to 2 b

13

00:00:41,550 --> 00:00:48,170

equals to 3, and then if you just do a

plus B and then you execute the script,

15

00:00:48,170 --> 00:00:58,739

let me open another terminal, you will

not get any output in there. The reason

17

00:00:58,739 --> 00:01:04,559

is that you are not printing out

anything in your program, so this is

19

00:01:04,559 --> 00:01:10,229

different from an interactive session.

Here if you simply do an operation or if

21

00:01:10,229 --> 00:01:16,650

you simply call a variable like that, you'll

get the output promptly, so this is just

23

00:01:16,650 --> 00:01:21,840

for simplicity when you when you try

things out, so you don't have to pass

25

00:01:21,840 --> 00:01:27,600

print explicitly, but Python will

understand that you met print. But here

27

00:01:27,600 --> 00:01:35,130

things are more strict, so you if you

want to print out the output, you want

29

00:01:35,130 --> 00:01:43,680

to say print a plus b and then execute

and you get the output and yeah basically

31

00:01:43,680 --> 00:01:48,960

that's the only difference between

executing a program from a Python file

33

00:01:48,960 --> 00:01:56,130

and running the code in the interactive

shell, so these are numbers and I can

35

00:01:56,130 --> 00:02:03,439

prove that if I do type 2 for instance.

You'll see that this is an INT

37

00:02:03,439 --> 00:02:10,289

type which means is an integer, so

basically we have integers and we have

39

00:02:10,289 --> 00:02:13,799

floats. These are the two main types

of numbers.

41

00:02:13,799 --> 00:02:22,379

A float is, let's say 2.4 it's a float

and I can prove that 2.4,

43

00:02:22,379 --> 00:02:30,359

type 2.4. You'll see that this

is a float, however if you do type 2 in

45

00:02:30,359 --> 00:02:35,609

brackets you'll see that this is a

string, it's not a number and therefore

47

00:02:35,609 --> 00:02:42,689

you cannot make operations using a

string and a number so Python will give

49

00:02:42,689 --> 00:02:47,430

you a type error and this is saying

cannot convert integer to string

51

00:02:47,430 --> 00:02:54,569

implicitly so when you do a string plus

something else Python expects that you

53

00:02:54,569 --> 00:03:00,000

were adding another string after a plus

sign, so that's why it says I cannot

55

00:03:00,000 --> 00:03:05,549

convert integer object, so it cannot

convert this to a string implicitly

57

00:03:05,549 --> 00:03:14,040

because if you were to do two plus three

you'd get 23 because Python treats these

59

00:03:14,040 --> 00:03:17,189

two as text. So to understand it

better

61

00:03:17,189 --> 00:03:26,729

let's add some text here b so you get

2a3b, so it's a concatenation operation.

63

00:03:26,729 --> 00:03:31,199

When the plus operator is used with

strings it is referred to as

65

00:03:31,199 --> 00:03:37,010

concatenation. Now let's go back to our

sample program that we used earlier

67

00:03:37,010 --> 00:03:44,549

which was this one in here, so now what

if instead of getting a greeting from

69

00:03:44,549 --> 00:03:53,220

the user you ask their for their age?

Enter your age, so this is just a message

71

00:03:53,220 --> 00:03:56,879

you can write anything you want there,

but just try to have a message that

73

00:03:56,879 --> 00:04:04,680

makes some sense to the user and so also

I'm going to change the variable name to

75

00:04:04,680 --> 00:04:11,400

something more meaningful, age and then

what I want to do is let's say new age,

77

00:04:11,400 --> 00:04:18,790

I want to calculate the age plus 50.

Why not?

79

00:04:18,790 --> 00:04:28,540

I'll print out the new age. Save the

script and let's go and execute this, we are

81

00:04:28,540 --> 00:04:33,130

going to get an error here I warn you.

But it's good that you see the errors

83

00:04:33,130 --> 00:04:36,030

that you get because that's how you

learn better.

85

00:04:36,030 --> 00:04:45,580

So Python my program, execute.

For now it's working fine because what

87

00:04:45,580 --> 00:04:51,280

happens is Python executes a script

line by line so it goes to the first

89

00:04:51,280 --> 00:04:56,980

line and if there is no error it

executes that line and in this case we have

91

00:04:56,980 --> 00:05:02,830

an input function so the program is on

standby mode so it expects from the user

93

00:05:02,830 --> 00:05:10,780

to enter the input, so let's enter the

input here. Let's say 28 and here is the

95

00:05:10,780 --> 00:05:16,420

error that we get. So again we saw this

error before. This is saying type error

97

00:05:16,420 --> 00:05:21,250

cannot convert int object to string

implicitly and you also have the line

99

00:05:21,250 --> 00:05:30,630

here that the error occurred. So these are

important messages and line two in here.

101

00:05:30,630 --> 00:05:37,270

So what's happening is that age is a

string it's not a number therefore

103

00:05:37,270 --> 00:05:43,180

python cannot add up a string with a

number and so why is age a string?

105

00:05:43,180 --> 00:05:48,520

Because that's what the input function

produces, so the input function get the

107

00:05:48,520 --> 00:05:55,000

user input and converts it to a string

so even if you pass a number there these

109

00:05:55,000 --> 00:05:59,140

will always convert everything to a

string.

111

00:05:59,140 --> 00:06:07,570

Therefore Python you know cannot

concatenate 28 plus 50 so what can you

113

00:06:07,570 --> 00:06:13,480

do in that case? Well you want to convert

that string into an integer and for

115

00:06:13,480 --> 00:06:19,360

that you want to use the int function,

so it's a Python function that does the work.

117

00:06:19,360 --> 00:06:27,570

As you can see it outputs now

the correct output so 28 plus 50 equals

119

00:06:27,570 --> 00:06:32,410

78 and similarly you can convert

numbers to strings

121

00:06:32,410 --> 00:06:42,280

and let's say str 50 and I think we'll

get an error here. Yeah, it says type error.

123

00:06:42,280 --> 00:06:48,580

Unsupported operand type four plus.

So you're adding up an integer with a

125

00:06:48,580 --> 00:06:57,850

string, so integer with a string. If you

like to convert this to a float, let me

127

00:06:57,850 --> 00:07:07,270

remove that, save, execute. 28. You get

78 point zero because what

129

00:07:07,270 --> 00:07:19,330

Python does when you use float is you

know float 28, it converts it to 28.0.

131

00:07:19,330 --> 00:07:24,160

So floats can be useful in certain scenarios.

And you'll have a lot of scenarios in the

133

00:07:24,160 --> 00:07:28,690

course, so don't worry about that now

because we have real world

135

00:07:28,690 --> 00:07:33,490

applications later on where you see the

real benefit of everything that you are

137

00:07:33,490 --> 00:07:39,040

explaining here in this early sections

that cover the basics. So this is about

139

00:07:39,040 --> 00:07:43,540

numbers. Try to practice them a little

bit and I'll talk to you in the next

141

00:07:43,540 --> 00:07:45,690

lecture.

Loading comments...