1
00:00:04,740 --> 00:00:08,300
To look at some of the Python operators
that we can use with numbers,

2
00:00:08,300 --> 00:00:11,640
We're going to continue with our Hello World project.

3
00:00:11,640 --> 00:00:22,820
So we want to create a new file here. We'll call this one operators.

4
00:00:22,820 --> 00:00:27,340
So what I'll do, is start by binding a couple of labels to two int values.

5
00:00:27,340 --> 00:00:31,540
Remember that you'll also hear that described as assigning the values to the variables,

6
00:00:31,540 --> 00:00:34,600
but that's not the best way of thinking about it in Python.

7
00:00:34,600 --> 00:00:40,440
So I'm going to type a = 12, and on the next line, b = 3.

8
00:00:40,440 --> 00:00:44,760
Whenever we use the name a, Python
knows that it refers to the value 12.

9
00:00:44,760 --> 00:00:48,380
Similarly, b will refer to the value 3.

10
00:00:48,380 --> 00:00:54,280
So let's create some expressions to see the basic Python arithmetic operators.

11
00:00:54,280 --> 00:01:01,140
So I'll start on line 4, print parentheses, and then within the parentheses, a + b.

12
00:01:01,140 --> 00:01:06,040
Next line, print parentheses again, this time a - b,

13
00:01:06,040 --> 00:01:09,060
and what I'll do is, actually, go back up to the previous line.

14
00:01:09,060 --> 00:01:17,340
I'll do a tab and I'll just put the expected results there. So 15 would be the value of the first one, the next one, 9.

15
00:01:17,340 --> 00:01:26,900
Alright line 6, parentheses after the keyword print, a * b,  and the value for that should be 36.

16
00:01:26,900 --> 00:01:31,620
Next line, print parentheses and then a / b,

17
00:01:31,620 --> 00:01:35,060
and that should give us the value 4.0.

18
00:01:35,060 --> 00:01:42,520
Next line, print, then in parentheses a integer division, which is two forward slashes with b,

19
00:01:42,520 --> 00:01:48,560
and that should give us the value of 4, and again it's integer division,

20
00:01:48,560 --> 00:01:56,140
and it's rounded down towards minus infinity.

21
00:01:56,140 --> 00:02:06,620
And the last line we'll do a, be consistent here with the formatting, a percent b, which is the remainder operator.

22
00:02:06,620 --> 00:02:13,180
and that's going to 0 modulo and it's the remainder

23
00:02:13,180 --> 00:02:16,900
after integer division.

24
00:02:16,900 --> 00:02:21,480
Alright, I'll just fix this line up as well, just so it's consistently spaced.

25
00:02:21,480 --> 00:02:23,380
Alright and there we have it.

26
00:02:23,380 --> 00:02:26,200
So probably, there shouldn't be any real surprises here,

27
00:02:26,200 --> 00:02:30,860
except possibly the use of an asterisk for multiplication, if you're new to programming languages.

28
00:02:30,860 --> 00:02:37,320
x is a valid name for a variable, which means we have to use the asterisk for multiplication.

29
00:02:37,320 --> 00:02:40,760
So just run the program first, to make sure we get the expected results,

30
00:02:40,760 --> 00:02:46,060
and you saw me add those as comments, in the, or for, each line. So we'll run it,

31
00:02:46,060 --> 00:02:50,460
and there's our results, as you can see, exactly as we've got written in the comments.

32
00:02:50,460 --> 00:02:53,400
Note though that although we're using int values,

33
00:02:53,400 --> 00:02:59,800
division produces a float result, 4.0, which you can see in the source code on line 7.

34
00:02:59,800 --> 00:03:04,580
Now if we use two forward slashes to perform integer division, then the result is an integer.

35
00:03:04,580 --> 00:03:09,020
You saw that on line 8 and we got the expected result 4, in this case.

36
00:03:09,020 --> 00:03:14,140
So that can be important if we intend to use that value in a place where an int must be used.

37
00:03:14,140 --> 00:03:19,100
So to give the example, I'm going to add some code to print a blank line, then the numbers 1 to 3.

38
00:03:19,100 --> 00:03:22,420
Now don't worry too much about understanding this next bit of code.

39
00:03:22,420 --> 00:03:28,980
I'll use one here to illustrate the point, but we're going to be discussing for loops in detail, in the next section in this course.

40
00:03:28,980 --> 00:03:33,100
For now, just make sure that you put 4 spaces before the word print,

41
00:03:33,100 --> 00:03:40,400
and a colon at the end of the for line, and you'll see what that means when I start typing now.

42
00:03:40,400 --> 00:03:47,840
Alright, so here I am now on line 11. I'm going to add print, and an empty parenthesis to print an empty line,

43
00:03:47,840 --> 00:03:57,520
or blank line, and then next I'm going to type for on line 13, for i in range, and parenthesis, 1, 4

44
00:03:57,520 --> 00:04:00,440
in the parenthesis, and then colon immediately after that.

45
00:04:00,440 --> 00:04:06,400
Now if I just press Enter, you can see that IntelliJ has tabbed that for us, but if that's not happened for you automatically,

46
00:04:06,400 --> 00:04:09,270
you'll need to put those four spaces in, as I mentioned.

47
00:04:09,270 --> 00:04:18,839
So print and parentheses, the letter i, and
if I run this, you can see we get the results 1 to 3.

48
00:04:18,839 --> 00:04:23,820
Now the values we used in range, and I'll just close down the run window now,

49
00:04:23,820 --> 00:04:26,880
there on line 13, must be integers.

50
00:04:26,880 --> 00:04:35,720
So if I change this to use a / b, come down here, so instead of 4, changing that to a / b,

51
00:04:35,720 --> 00:04:42,020
we get an error there, or we will get an error when we run it.

52
00:04:42,020 --> 00:04:47,500
As you can see, we've got a type error there: float object cannot be interpreted as an int

53
00:04:47,500 --> 00:04:55,600
So performing integer division, using two forward slashes instead of one, would actually prevent this problem,

54
00:04:55,600 --> 00:05:00,120
and you can see we get the numbers 1 2 3 and the warning has now disappeared.

55
00:05:00,120 --> 00:05:07,560
So essentially, we change the code to perform integer division and the result of a // b, two foreward slash b,

56
00:05:07,560 --> 00:05:08,740
is now an int.

57
00:05:08,740 --> 00:05:13,580
We'll see another way to cope with situations like this, but for now it's something you need to be aware of.

58
00:05:13,580 --> 00:05:19,660
Python is strongly typed, and you can't use a float in places where an int must be used.

59
00:05:19,660 --> 00:05:26,240
Alright, so code like a + b, as I've done up here on line 4, is called an expression.

60
00:05:26,240 --> 00:05:28,720
In the next video, we'll learn what expressions are.

61
00:05:28,720 --> 00:05:31,580
See you in the next video.

