00:01 - 00:06

hey guys so welcome to the second video

00:04 - 00:08

in the series um if you remember in the

00:06 - 00:11

last video we talked about data types

00:08 - 00:13

and variables uh we went briefly into

00:11 - 00:15

some of the things that python can do um

00:13 - 00:17

today I'm going to be talking about

00:15 - 00:20

operators and we're going to be doing

00:17 - 00:23

some print statements some input um a

00:20 - 00:24

little a few small console applications

00:23 - 00:27

just applying some of the knowledge that

00:24 - 00:28

we've learned so pretty much the first

00:27 - 00:29

thing I want to do is I want to go back

00:28 - 00:31

and I want to review what we did in the

00:29 - 00:32

last lesson very quickly so we talked

00:31 - 00:34

about variables and data types so for

00:32 - 00:37

example the X variable we could set it

00:34 - 00:39

to something equal to two like that uh a

00:37 - 00:42

name we may equal to our name right so I

00:39 - 00:44

make that equal to Tim um so there we go

00:42 - 00:47

we have the name right there equal to

00:44 - 00:48

Tim we'll start with that okay now if

00:47 - 00:49

you remember in the last one I didn't

00:48 - 00:52

talk about this but I used something

00:49 - 00:54

called print now the print statement

00:52 - 00:57

pretty much takes an argument so inside

00:54 - 00:58

of these brackets the thing that is

00:57 - 01:02

inside that brackets is called the

00:58 - 01:04

argument so so it takes a uh a string

01:02 - 01:06

argument usually so I will give it the

01:04 - 01:08

name so now you'll see again we did this

01:06 - 01:10

in the last one just want to review I'm

01:08 - 01:12

clicking F5 simply to run the program

01:10 - 01:15

when I go to the console it simply puts

01:12 - 01:19

Tim onto the console just like that

01:15 - 01:22

right okay um very basic we already did

01:19 - 01:25

that so now let's say well we want to do

01:22 - 01:26

something else so let's let's print here

01:25 - 01:27

and we're just going to put a string and

01:26 - 01:31

we're just going to type this in

01:27 - 01:33

ourselves so let's say hello comma what

01:31 - 01:35

is your

01:33 - 01:38

name that's going to print to the

01:35 - 01:41

console now we want to get what the

01:38 - 01:43

user's name is now there's a way to do

01:41 - 01:44

this in Python it's very simple we're

01:43 - 01:47

going to make a variable name it makes

01:44 - 01:49

sense to put the variable name here do

01:47 - 01:52

an equal sign because it's a variable

01:49 - 01:55

right and we're going to type the word

01:52 - 01:57

input just like this now what's going to

01:55 - 02:00

happen if I run the program here I'll

01:57 - 02:02

show you is it's going to say hello what

02:00 - 02:05

is your name and it's going to actually

02:02 - 02:06

allow me now to type into the console

02:05 - 02:08

which I couldn't do before now nothing

02:06 - 02:09

happens after when I click enter because

02:08 - 02:11

we don't have anything else after that

02:09 - 02:15

but it's allowing me to

02:11 - 02:17

type okay so now well we want to print

02:15 - 02:21

out what the name is that was said so

02:17 - 02:23

let's just simply type print and then

02:21 - 02:26

name all right and we have hello what is

02:23 - 02:28

your name I say oh well my name's Tim it

02:26 - 02:31

says Tim all

02:28 - 02:32

right now well we just printed out the

02:31 - 02:34

name but maybe we want to go a little

02:32 - 02:36

more advanced than that we want to say

02:34 - 02:38

well hello Tim how are you doing Tim

02:36 - 02:41

something like that right so I'm going

02:38 - 02:43

to just put a comma here going to

02:41 - 02:45

separate these two things and I'm going

02:43 - 02:46

to put in a string and I'm just going to

02:45 - 02:49

type it like the type so I'm going to

02:46 - 02:52

say hello with a comma and then you see

02:49 - 02:53

that I have this other comma here that

02:52 - 02:55

is outside of the quotations meaning

02:53 - 02:57

it's not a string it's actually just

02:55 - 02:59

separating the two arguments in here and

02:57 - 03:02

you'll notice what happens now what what

02:59 - 03:04

I print is it goes hello what is your

03:02 - 03:07

name so I say oh my name's Tim it says

03:04 - 03:09

hello Tim right so that's pretty

03:07 - 03:10

straightforward I hope that's easy to

03:09 - 03:12

understand we're just using the input to

03:10 - 03:15

get an input and then we're printing it

03:12 - 03:16

back out to the screen okay so let's do

03:15 - 03:18

um let's go into the next part of this

03:16 - 03:21

now we'll use that again later but let's

03:18 - 03:24

talk about operators so operator that

03:21 - 03:27

term may sound familiar to you um in

03:24 - 03:28

math uh we use things called operators

03:27 - 03:30

so these four operators hopefully should

03:28 - 03:32

look familiar to you

03:30 - 03:34

um this is a plus sign this is the

03:32 - 03:37

addition operator we have the minus sign

03:34 - 03:39

which is the subtraction operator the

03:37 - 03:41

division sign which is the well division

03:39 - 03:43

operator and then the multiplication

03:41 - 03:46

sign which is again the multiplication

03:43 - 03:47

uh operator so there's a few more

03:46 - 03:50

operators that we'll talk about but

03:47 - 03:51

these are the four basic ones in Python

03:50 - 03:53

now you remember how I talked to you

03:51 - 03:56

about data types this is where this

03:53 - 03:57

comes in when we use these operators

03:56 - 04:00

it's important that we use them on

03:57 - 04:03

certain data types so for example in

04:00 - 04:05

math I could do something like 3 + 4

04:03 - 04:08

right so what is 3 + 4 equal well that

04:05 - 04:10

is equal to 7 so the computer can

04:08 - 04:12

actually do that math operation by using

04:10 - 04:14

that operator to return those values so

04:12 - 04:18

let's let's give an example here quickly

04:14 - 04:20

so I have uh let's do num one we're

04:18 - 04:21

going to use variables here uh remember

04:20 - 04:23

I can use a number in the variable as

04:21 - 04:25

long as it's not at the beginning and

04:23 - 04:27

I'm going to give it a value of 45 and

04:25 - 04:30

then we'll do num two and we're going to

04:27 - 04:33

give it a value of three now if I want

04:30 - 04:37

to print to the console um let's say num

04:33 - 04:38

one plus num 2 well you can guess what

04:37 - 04:44

that's going to give us it's going to

04:38 - 04:47

give us 45 + three I hope if this is not

04:44 - 04:49

error yeah so that gave us 48 right so

04:47 - 04:52

oops didn't mean to make that full

04:49 - 04:55

screen num one plus num 2 is 48 all

04:52 - 04:59

right so now what about minus let's try

04:55 - 05:01

this 42 basic um we can do

04:59 - 05:02

multiplication here with the

05:01 - 05:06

multiplication

05:02 - 05:08

sign there we go 135 and then we'll try

05:06 - 05:10

again with the division sign just to

05:08 - 05:14

show you all of them we get

05:10 - 05:16

15.0 like that okay so that's pretty

05:14 - 05:18

straightforward um those four basic

05:16 - 05:20

operators now in Python there's a few

05:18 - 05:23

more operators that we want to talk

05:20 - 05:24

about now for example in math we have

05:23 - 05:27

something called exponents right so how

05:24 - 05:30

do we do exponents in Python well it's

05:27 - 05:32

actually two stars is how you do an

05:30 - 05:34

exponent all right and now maybe there's

05:32 - 05:35

something called integer division um

05:34 - 05:37

which I'll get into later but I'll just

05:35 - 05:40

show you the operator for it right now

05:37 - 05:42

it's two slashes that means uh I'll give

05:40 - 05:44

you an example a math example here

05:42 - 05:47

64 divided

05:44 - 05:51

by um let's do 10 um would give us

05:47 - 05:53

usually in math a value of 6.4 right

05:51 - 05:56

that's if you use one division sign

05:53 - 05:59

sorry what am I doing times I mean

05:56 - 06:01

divided by would give us 64 I have 6.4

05:59 - 06:03

but now if we do two division signs here

06:01 - 06:05

like this it actually gives us a value

06:03 - 06:07

of six that's because it just um it

06:05 - 06:10

doesn't worry about the remainder at all

06:07 - 06:14

it just tells us um how many times 10

06:10 - 06:16

can go into 64 um and that's all it can

06:14 - 06:18

go in six times evenly so it gives us a

06:16 - 06:21

whole number as our answer that's called

06:18 - 06:22

integer division this double double

06:21 - 06:25

slash like

06:22 - 06:28

this okay and then we have another

06:25 - 06:30

operator which is actually the modulus

06:28 - 06:33

operator um so this is the percentage

06:30 - 06:38

sign and this gives us the remainder so

06:33 - 06:41

here if I do five modulus

06:38 - 06:45

2 then the remainder of that is actually

06:41 - 06:47

1 because 5 ID 2 is 4 then with a

06:45 - 06:48

remainder of one right so it's not going

06:47 - 06:49

to give us the decimal points it's again

06:48 - 06:52

just going to give us the remainder if I

06:49 - 06:54

did five ID four again the remainder is

06:52 - 06:56

one 5id by three the remainder would be

06:54 - 06:58

two like that okay so that's what the

06:56 - 07:00

modulus sign um gives us so we'll put

06:58 - 07:03

modulus here and then there's probably a

07:00 - 07:04

few others that I'm forgetting about but

07:03 - 07:07

we can go into those

07:04 - 07:09

later so now um same thing let's keep

07:07 - 07:11

using our number variables here and now

07:09 - 07:14

let's introduce a third variable called

07:11 - 07:17

num three now um I want to show you how

07:14 - 07:18

we can use the variables so num one and

07:17 - 07:21

let's see if you remember what this

07:18 - 07:22

operator is uh so that's the exponent

07:21 - 07:25

operator so that means num one which is

07:22 - 07:26

going to be our base uh which is 45

07:25 - 07:28

raised to the power of four because

07:26 - 07:30

we're going to use num two and we can

07:28 - 07:32

print

07:30 - 07:34

num

07:32 - 07:36

three press

07:34 - 07:38

okay and you can see we get a pretty

07:36 - 07:40

large number um that's because of how

07:38 - 07:42

exponents work and

07:40 - 07:45

then same thing here if we want to do

07:42 - 07:46

maybe integer division see what we're

07:45 - 07:49

going to

07:46 - 07:53

get we get 11 right a whole number and

07:49 - 07:53

then say we want to do

07:55 - 07:59

modulus we get one okay so now let's tie

07:58 - 08:01

all of these things together with

07:59 - 08:02

operators that we've used the variables

08:01 - 08:06

the prints and the

08:02 - 08:08

inputs so um let's start by just get

08:06 - 08:11

doing a little print statement and

08:08 - 08:13

saying pick a

08:11 - 08:16

number okay pretty basic we're going to

08:13 - 08:17

pick a number and then we're going to

08:16 - 08:21

take the input so we're going to say num

08:17 - 08:21

one again is equal to the input of

08:22 - 08:30

that okay so we got the input in Num one

08:25 - 08:30

um now let's say print

08:30 - 08:34

pick another

08:34 - 08:37

number all right so now we're going to

08:36 - 08:41

pick another number so we're going to do

08:37 - 08:42

num two is equal to input like that and

08:41 - 08:43

now what we're going to do is we're

08:42 - 08:45

going to introduce our third variable so

08:43 - 08:49

we'll call it sum because we're going to

08:45 - 08:51

add these is equal to num one plus num

08:49 - 08:52

two and now actually you can't use sum

08:51 - 08:55

because it's a reserve word so let's

08:52 - 08:56

just do in all capitals some because

08:55 - 08:58

remember how we talked about variables

08:56 - 09:00

capitals and lowercase are different and

08:58 - 09:05

then we're going to print to the

09:00 - 09:07

console the sum like that

09:05 - 09:09

okay so we run the program says pick a

09:07 - 09:13

number uh let's pick a number let's say

09:09 - 09:14

four and let's do 32 now what do you

09:13 - 09:19

think it's going to

09:14 - 09:22

be oh oops okay

09:19 - 09:24

so yeah someone equal num one plus num

09:22 - 09:26

two okay so this is why um I talked

09:24 - 09:28

about data types so I'm actually uh

09:26 - 09:30

Happy this happened it's because what

09:28 - 09:32

actually happens when we get the input

09:30 - 09:36

of something is it gives us the type of

09:32 - 09:39

a string um which in this case is not

09:36 - 09:41

what we wanted to do so um you saw there

09:39 - 09:43

I'll run it again um it's a good mistake

09:41 - 09:45

that this happened actually we have four

09:43 - 09:47

and we have three and it gave us 43

09:45 - 09:50

that's just because we added the string

09:47 - 09:52

4 to the string three um so that simply

09:50 - 09:54

gave us 43 but now we know in actual

09:52 - 09:57

math that 4 + 3 equal 7 so if we want to

09:54 - 09:59

do um the integers we have to actually

09:57 - 10:01

convert these variables into integers

09:59 - 10:05

so in order to do that remember I showed

10:01 - 10:06

you the keyword um int like this before

10:05 - 10:10

we're just going to put brackets around

10:06 - 10:14

our two variables here like this

10:10 - 10:15

integers and now hopefully we should get

10:14 - 10:17

the correct answer when we do four and

10:15 - 10:19

we do three and you can see we've got

10:17 - 10:21

seven so um I didn't mean for that to

10:19 - 10:24

happen originally but I'm happy it did

10:21 - 10:26

because it shows us why data types are

10:24 - 10:28

important so when we take the input of

10:26 - 10:30

something we're typing it from the

10:28 - 10:33

keyboard and that is actually a string

10:30 - 10:34

so um here we can print again um don't

10:33 - 10:36

worry about what I'm doing right now but

10:34 - 10:37

I just want to show you the type which

10:36 - 10:39

is going to give us if it's a string if

10:37 - 10:43

it's an integer of num two just to show

10:39 - 10:45

you what the type actually is so we pick

10:43 - 10:48

a number we pick two and we pick three

10:45 - 10:51

it tells us that three is actually a

10:48 - 10:53

string so that's why when we added

10:51 - 10:55

originally three and two or two and

10:53 - 10:57

three or whatever it was um it gave us

10:55 - 10:59

just them added together so two and

10:57 - 11:02

three rather than what it should be

10:59 - 11:05

which is five okay now same thing here

11:02 - 11:08

if we had a number and we wanted to

11:05 - 11:11

convert it into a string all we would do

11:08 - 11:13

is we would type Str Str around the num

11:11 - 11:17

so for example three and then that would

11:13 - 11:19

give us three just like that so A String

11:17 - 11:22

so now we've kind of gone over

11:19 - 11:24

conversion of variables um conversion of

11:22 - 11:27

numbers data types how to get input from

11:24 - 11:30

the console using the input like this um

11:27 - 11:33

and some basic operators of python

11:30 - 11:35

so uh review this I hope you followed

11:33 - 11:37

along and then in the next lesson we'll

11:35 - 11:40

move into some more advanced operators

11:37 - 11:42

and we'll maybe start with conditions

11:40 - 11:44

okay I hope you enjoyed uh please

11:42 - 11:48

subscribe like the video and I'll see

11:44 - 11:48

you in the next video

Understanding Python Operators: A Beginner's Guide

In this article, we will dive into the world of Python Operators. We will explore the basic operations you can perform with Python, how to handle user inputs, and print outputs on the console. By the end, you'll learn how operators work in Python and gain practical experience in applying these concepts through small console applications. Let's get started!

What are Python Operators?

Operators are symbols that perform operations on variables and values. In Python, just like in mathematics, we have multiple types of operators. The basic operators you'll encounter are:

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)

These foundational operators allow us to perform arithmetic calculations with numbers and variables.

Understanding Variables and Input

Before we get into operators, let’s take a moment to review variables and input in Python. A variable is a container for data; you assign a value to it, such as:

x = 2
name = "Tim"

To receive input from a user, we utilize the input() function, which allows us to capture data from the console. For instance:

user_name = input("Hello, what is your name? ")
print("Hello " + user_name)

When run, this code snippet greets the user and displays their input as part of the greeting.

Working with Basic Operators

Now, let's apply some basic operations using our variables. Suppose we have two variables defined as:

num1 = 45
num2 = 3

You can perform operations and then print the results:

print(num1 + num2)  # Outputs: 48
print(num1 - num2)  # Outputs: 42
print(num1 * num2)  # Outputs: 135
print(num1 / num2)  # Outputs: 15.0

Advanced Operators in Python

Beyond the four basic operators, Python supports additional operations that are essential for more complex calculations. These include:

  1. Exponentiation: Raise a number to the power of another. In Python, it's represented by **, so num1 ** num2 would compute ( 45^3 ).

  2. Integer Division: Use // to perform division without returning a decimal. For example, 64 // 10 results in 6.

  3. Modulus: The modulus operator (%) returns the remainder of a division. For example, 5 % 2 gives 1, because 5 divided by 2 leaves a remainder of 1.

Combining Operators, Input, and Variables

Now that we understand the basics of operators, let's create a small console application that utilizes both user input and arithmetic operations.

print("Pick a number:")
num1 = int(input())  # Convert input string to integer
print("Pick another number:")
num2 = int(input())
sum_result = num1 + num2
print("The sum is: " + str(sum_result))  # Convert sum back to string for printing

Importance of Data Types

As you work with inputs, it's essential to recognize that all the data received from input() is treated as strings. This can lead to unintended results, such as concatenating strings rather than performing numerical addition:

# This will concatenate because they're both strings
# If num1 is "4" and num2 is "3", the output would be "43"
concat_result = user_input_num1 + user_input_num2

To avoid this, you must convert these inputs to integers using the int() function before performing any arithmetic operations.

Conclusion: The Power of Operators

As you can see, operators are a crucial part of programming in Python, allowing you to manipulate data effectively. Armed with your new knowledge of operators, variables, and inputs, you're ready to tackle more advanced topics like conditional statements and loops.

In the upcoming lessons, we'll delve deeper into complex operators and functions. Remember, practice is key—so don't hesitate to experiment with the concepts you've learned today! Happy coding!