#11 Python Tutorial for Beginners | Operators in Python

Telusko
6 Jul 201811:06

Summary

TLDRIn this Python tutorial, Ivan Vetti explores various operators including arithmetic, assignment, relational, and logical. He demonstrates arithmetic operations with variables and shows how to use assignment shortcuts like '+=' for incrementing values. Relational operators are explained with examples comparing variables, using symbols like '<', '>', '==', and '!='. Logical operators 'and', 'or', and 'not' are also covered, illustrating how to combine conditions using truth tables. The video serves as a foundational guide for more complex coding in future tutorials.

Takeaways

  • 😀 The video series on Python continues with a focus on various types of operators.
  • 🔱 Arithmetic operators such as addition, subtraction, multiplication, and division are covered, demonstrating how to perform basic mathematical operations using variables.
  • ➕ Assignment operators are explained, showing how to assign values to variables and how to use shorthand operators like `+=` for incrementing values.
  • 🔄 The concept of unary operators is introduced, with an example of negation using the minus sign to reverse the sign of a number.
  • 🔗 Relational operators like `less than`, `greater than`, `equal to`, and `not equal to` are discussed, illustrating how to compare values.
  • 🔀 Logical operators `and`, `or`, and `not` are explained, showing how to combine multiple conditions in decision-making.
  • 📊 A truth table is used to demonstrate the behavior of logical operators, helping to understand how true and false values interact.
  • đŸ’» The video emphasizes that understanding these operators is foundational for writing more complex Python code in the future.
  • đŸŽ„ The presenter, Ivan, engages viewers by encouraging them to follow along with the series for more advanced topics.
  • 📝 The video serves as an educational resource, aiming to build a strong base for viewers to tackle more complex programming concepts.

Q & A

  • What are the different types of operators discussed in the video?

    -The video discusses arithmetic operators, assignment operators, relational operators, logical operators, and unary operators.

  • What is the purpose of arithmetic operators in Python?

    -Arithmetic operators in Python are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.

  • How does the assignment operator work in Python?

    -The assignment operator in Python is represented by the '=' symbol, which is used to assign a value to a variable.

  • What is the shortcut for adding a value to a variable in Python?

    -In Python, the shortcut for adding a value to a variable is using the '+=' operator, which increments the variable by the specified value.

  • Can you assign values to multiple variables in a single line in Python?

    -Yes, in Python, you can assign values to multiple variables in a single line using a comma-separated list of variables and values.

  • What is a unary operator and how is it used in Python?

    -A unary operator is an operator that takes only one operand. In Python, the unary operator '-' is used to negate a value, making it negative.

  • What are relational operators and how are they used to compare values?

    -Relational operators are used to compare two values and determine their relationship. They include operators like '<' for less than, '>' for greater than, '==' for equality, and '!=' for inequality.

  • What is the logical operator 'and' used for in Python?

    -The logical operator 'and' is used in Python to combine two conditions, and the result is true only if both conditions are true.

  • How does the logical operator 'or' differ from 'and' in Python?

    -The logical operator 'or' in Python returns true if at least one of the conditions is true, unlike 'and' which requires both conditions to be true.

  • What is the purpose of the logical operator 'not' in Python?

    -The logical operator 'not' in Python is used to reverse the boolean value of an expression, turning true to false and vice versa.

Outlines

00:00

🐍 Python Basics: Understanding Operators

In this segment, Ivan introduces various operators in Python, starting with arithmetic operators like addition, subtraction, multiplication, and division. He demonstrates these using variables X and Y set to 2 and 3, respectively. Ivan then moves on to assignment operators, explaining how to assign values to variables and how to use shorthand operators like '+=' for incrementing values. He also covers the concept of unary operators with the example of negation using the '-' sign. The paragraph is a foundational lesson on how to manipulate and compare data using Python's built-in operators.

05:01

🔍 Deep Dive into Relational and Logical Operators

This part of the video script delves into relational operators, explaining how to compare values using '<', '>', '==', '!=', '<=', and '>='. Ivan uses variables a and b with values 5 and 6 to illustrate these comparisons, showing how boolean values of 'True' and 'False' are returned based on the comparison results. The paragraph then transitions into logical operators, discussing how to combine multiple conditions using 'and', 'or', and 'not'. Ivan explains the concept using truth tables and provides examples to demonstrate how these operators work in practice, emphasizing their importance in programming for creating complex conditional statements.

10:02

🎓 Wrapping Up: Foundation for Advanced Python Concepts

In the concluding paragraph, Ivan summarizes the importance of understanding basic operators as a foundation for more advanced Python programming. He encourages viewers to stay engaged for future videos that will build upon these basics, promising to cover more complex coding concepts and patterns. Ivan invites viewers to share their thoughts in the comments and thanks them for watching, hinting at more informative and enjoyable content to come in the series.

Mindmap

Keywords

💡Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. In the video, these operators include addition, subtraction, multiplication, and division. They are fundamental in programming for manipulating numerical values. For instance, the script mentions 'X plus Y' and 'X divided by Y' to illustrate how to add and divide two variables, respectively.

💡Assignment Operators

Assignment operators are used to assign values to variables. The video explains that the equal sign (=) is the basic assignment operator, as seen in 'X equals 8'. It also introduces shorthand operators like 'X plus equals 2', which increments the value of X by 2, demonstrating a more efficient way to update variable values.

💡Unary Operator

A unary operator is an operator that takes only one operand. The video discusses the negation concept using the unary operator, which is the minus sign. For example, 'n equals minus n' is used to negate the value of n, turning a positive number into a negative one, which is a straightforward application of the unary operator.

💡Relational Operators

Relational operators are used to compare two values. The script covers operators like 'less than' and 'greater than', and also 'less than or equal to' and 'not equal to'. These operators return a Boolean value (True or False) based on the comparison. For instance, 'a is less than B' is used to check if the value of a is smaller than B.

💡Logical Operators

Logical operators are used to combine multiple conditions. The video introduces 'and', 'or', and 'not' as logical operators. These operators are crucial for complex decision-making in programming. For example, 'a less than 8 and B less than 5' combines two conditions using the 'and' operator to ensure both must be true for the overall condition to be true.

💡Modulus Operator

The modulus operator is used to find the remainder of a division operation. While not explicitly detailed in the script, it is mentioned in passing as one of the arithmetic operators. It's used in scenarios where you need to find out how many times one number is divisible by another without carrying over.

💡Boolean Values

Boolean values are used to represent logical values, either True or False. The video script refers to these when discussing relational and logical operators, which often result in a Boolean outcome. For example, 'a is less than B' evaluates to True or False based on the values of a and B.

💡Truth Table

A truth table is a tool used in logic to map out all possible logical combinations of a set of inputs and their corresponding outputs. The video briefly mentions a truth table in the context of explaining how logical operators like 'and', 'or', and 'not' work, helping to visualize the outcomes of combining conditions.

💡Increment

Incrementing is the process of increasing a variable's value by a certain amount, often by one. The video uses 'X plus equals 2' as an example of incrementing the value of X by 2, which is a common operation in programming for iterating through loops or counting.

💡Negation

Negation is the act of inverting or reversing a statement. In programming, this is often used to reverse the sign of a number or to logically negate a Boolean value. The script demonstrates this with 'n equals minus n', changing the sign of n from positive to negative.

Highlights

Introduction to working with operators in Python.

Explanation of arithmetic operators: addition, subtraction, division, and modulus.

Demonstration of how to perform arithmetic operations with variables.

Introduction to assignment operators and their usage.

Example of incrementing a variable using a shortcut operator.

Explanation of how to perform compound assignments with multiplication and division.

Assignment of multiple variables in a single line of code.

Introduction to unary operators and their use in negating values.

Explanation of relational operators for comparing values.

Example of using 'less than' and 'greater than' operators.

Usage of 'equal to' and 'not equal to' operators for value comparison.

Combining relational operators to check for 'less than or equal to' and 'greater than or equal to'.

Introduction to logical operators and their role in combining conditions.

Explanation of the 'and' logical operator and its truth table.

Usage of the 'or' logical operator and its truth table.

Demonstration of the 'not' logical operator for negating conditions.

Emphasis on the importance of understanding logical operators for future programming concepts.

Encouragement for viewers to engage with the series and look forward to more complex coding topics.

Transcripts

play00:00

[Music]

play00:03

welcome back aliens my name is Ivan

play00:05

vetti and let's continue with this

play00:06

series on Python so till this point we

play00:09

have covered a lot of stuff right we

play00:10

know how to work with ible now we know

play00:13

how to perform certain operations and in

play00:15

this video we'll try to work with

play00:17

operators now one of some of the

play00:19

operators we have used white we have

play00:21

worked with arithmetic operators which

play00:23

is your addition subtraction division

play00:24

right now we also have some more

play00:27

operators which we are going to say in

play00:29

this video so we'll talk about automatic

play00:32

operators assignment operators

play00:34

relational operators and logical

play00:36

operators and we have one obvious unary

play00:38

operator so let's start with automatic

play00:40

in fact we have seen automatic right we

play00:42

have worked with addition subtraction

play00:44

they are very simple right so in fact we

play00:46

have done that before

play00:47

so I'll be using some variables here

play00:48

let's say X equal to 2 and Y is equal to

play00:51

3 so we got 2 variables and down to

play00:53

perform certain operations and you can

play00:55

do that right we have done that before

play00:57

so you can say X plus y you can say X

play00:59

minus y X multiplied by Y I guess I just

play01:03

pressed full by mistake and then when

play01:06

you say X divided by Y even that works

play01:08

you can see record the output so this is

play01:10

how you work with automatic operators we

play01:12

have also seen how to use modulus now

play01:14

next one is assignment and now you know

play01:17

like how assignment works we can simply

play01:19

say X equal to 8 and that's your

play01:21

assignment operator so any way you use

play01:23

equal to symbol

play01:24

that's assignment but then let's try to

play01:26

do something else what if I say I can

play01:29

add two numbers like X equal to X plus 2

play01:32

so what I am doing here is I am adding

play01:34

the value X itself with 2 so which means

play01:37

if my x value is let's say 2 plus 2

play01:40

which is 4 right actually I get that's

play01:42

what I am expecting here so if I say X

play01:44

we got 4 now the same operation you can

play01:47

do in a shortcut way you know we love

play01:50

shortcuts so here also we have some

play01:52

shortcuts which is if you want to do a

play01:54

same thing you can say X I want to

play01:56

increment okay what is increment

play01:57

enhancing the values so you will say X

play02:00

plus I want to increment myself with 2

play02:02

so you can say X plus equal to 2 so you

play02:05

are incrementing yourself by 2 and now

play02:08

if I say X you can see because the value

play02:10

which is set so which is so X was 4

play02:12

initially

play02:13

and then I'm incrementing by two again

play02:15

so which we got six so instead of saying

play02:17

X equal to X plus two we can say X plus

play02:20

equal to two the same thing can be done

play02:22

with subtraction division multiplication

play02:23

let's try

play02:25

so we'll say X multiplied equal to three

play02:28

so what we are doing is we are

play02:30

multiplying X by itself so X is six now

play02:33

and when you say 6 into 3 we got 18 is

play02:37

that yes that's right okay so this thing

play02:40

is working you can do short and s but

play02:41

you can try division and subtraction by

play02:44

yourself can we do assignment like this

play02:46

let's say we have two variables here a

play02:48

and B I want to assign value let's say

play02:50

five and six to a and B so we can write

play02:53

in two lines all right we can say a

play02:54

equal to 5 and B equal to 6 all you can

play02:56

do that in one line you can say a comma

play02:58

B is equal to 5 comma 6 so you can do

play03:01

assignment in one line itself so if I

play03:03

say enter there is no error if I try to

play03:06

fetch the value of a which is 5 if you

play03:07

want to try to try to fetch the value of

play03:09

B which is 6 so yes you can assign the

play03:12

values in one line for two variables so

play03:14

that's about assignment operator now

play03:17

with this let's move towards the next

play03:18

operator which is a unary operator

play03:21

now when I say unity operator it means

play03:23

one right so we have binary coin set

play03:25

which is to unity is 1 now how do you

play03:28

work with unity operator on which one we

play03:29

can use so let's say we have this

play03:32

concept of negation right example if I

play03:34

have a value which is 7 if I were to

play03:35

negate it so what I will do is I will

play03:37

use a variable which is n is equal to 7

play03:40

so we got n as 7 now we can also use so

play03:44

now we can negate it with the help of

play03:46

minus n so when you say minus n so if I

play03:49

if I say the value of NH to the 7 right

play03:51

so what you can do is if you want to

play03:52

assign the negative value you can say n

play03:54

is equal to minus n and if I say enter

play03:57

and if I print the value of n now which

play03:58

is minus 7 so you can you know put a

play04:01

negative sign just by giving a unit

play04:03

operator which is - so that's your basic

play04:06

operators assignment and unity the next

play04:09

two operators which we have is or two

play04:11

kinds of operators which we have is

play04:12

logical operators and relational

play04:15

operators they start with rational first

play04:16

now what is relational now in fact we

play04:19

have tons one of the thing before right

play04:21

in one of this one of the video where we

play04:22

can compare two numbers or we can

play04:24

compare two stuff so smaller great

play04:26

right or compel equal to so let's try

play04:29

here so there again we'll go for two

play04:30

variables here which is a and B which we

play04:32

have done before and of course the value

play04:34

of a is five and the value of B is six

play04:37

so can I do this I want to check if a is

play04:40

less than B so you can say a is less

play04:42

than B and answer is yes right because a

play04:44

is 5 and B is 6 so when I say enter now

play04:48

it says true and if you remember we have

play04:50

done that in boolean so it returns our

play04:52

true and false so if we when you use

play04:54

this angular symbol it's less than we

play04:56

can also use greater than symbol right

play04:58

now what if the value of a and B of

play05:00

course they are not same so how do you

play05:02

check for these say comparison if they

play05:03

are same in because if we cannot use

play05:05

single or single equal to because single

play05:07

equal to means you are assigning it we

play05:10

are not assigning here we are comparing

play05:12

it so in that case you will be using

play05:14

double equal to and then you will say B

play05:16

and you can see the got false and of

play05:18

course right a is 5 and B is 6 so of

play05:20

course it will give you false now in

play05:22

that case what you will do is you will

play05:24

say let's change the value of a I will

play05:26

say a is also 6

play05:28

so now B is 6 a is 6 let's perform the

play05:31

same same operation I will say a is

play05:32

equal to equal to B and you can see we

play05:34

got true now that same light so it will

play05:37

give you true I want to check if a is

play05:39

less than or equal to B can they do both

play05:43

the same time less than and equal to yes

play05:45

we can so we can say a less than equal

play05:48

to B so we can use this symbol in fact

play05:51

in maths we also have a symbol right so

play05:53

the same way we have in programming we

play05:55

can just put less than and equal to the

play05:57

same thing will be done for greater than

play05:59

equal to of course it's not greater

play06:01

unless it's equal to so it will compare

play06:03

either less or equal to and you can see

play06:05

it works now we can also also check if

play06:08

they are not equal now how do we check

play06:09

not equal let's say something else and

play06:12

be something else and I want to check if

play06:14

there are if they're not equal in that

play06:16

case you will use not equal to B so not

play06:19

is explanation so in programming

play06:21

remember this thing whenever you see

play06:23

exclamation that's that's are not simple

play06:24

so we got a not equal to B if I say

play06:27

enter you can see we got false why false

play06:29

is because your a is equal to P right so

play06:33

when you say not equal to it it doesn't

play06:35

make sense it it is false but yes the

play06:37

value of a or b is let's say

play06:39

7.now so we have a and B with different

play06:41

values if I perform the same operation a

play06:43

not equal to B you can see record true

play06:45

right so this are called as relational

play06:48

because you are trying to relate to

play06:50

different values the next set we have is

play06:52

logical operators

play06:54

so before understanding what logical

play06:55

operators are you to understand the

play06:57

concept of true and false and relation

play07:00

between so if you have two conditions

play07:01

and if you want to come and if you want

play07:03

to combine those two conditions now it

play07:06

can be based on and or it can be based

play07:08

on all or it can be not so basically we

play07:11

have three operators here and all and

play07:14

not now let's say we have two conditions

play07:16

one is related to a or we can say if a

play07:19

let me explain this shell once so let's

play07:22

again start with variables we can say

play07:23

here we got two variables will say a is

play07:26

equal to 5 and B is equal to let's say 4

play07:28

we got 2 variables okay and individually

play07:31

I want to check their preference example

play07:33

if a is less than 8 and I also want to

play07:38

check B should not be greater than 5 how

play07:40

do I check both and I want to make sure

play07:42

that a should be less than 8 and B

play07:45

should be less than 5

play07:46

in that case you need to rewrite two

play07:49

conditions one is a less than 8 and B is

play07:52

less than 5 so we have two conditions

play07:54

here what should I put in between so in

play07:57

that case you can use a logical

play07:59

operators which is and so I want to

play08:02

compare I want to check both right so I

play08:03

would say and and you can see we got

play08:05

true because both are true now what if I

play08:08

say I want to check if a is less than 8

play08:11

and B is less than 2 what do you think

play08:14

what is the output is it true or false

play08:16

see now the first condition is true

play08:18

right

play08:19

and second is false because B is not

play08:21

less than 2 in that case you will think

play08:23

okay one exploit should give you true

play08:25

but unfortunately get false

play08:26

now why it's the case because for to

play08:29

understand that we have a table here now

play08:31

this table is also called a truth table

play08:33

you can represent true with one and

play08:35

Falls with zero this is how it looks

play08:37

like so when you have let's say we have

play08:39

x and y so X represents one condition

play08:42

output and while present second an

play08:43

output will compare this so if X is zero

play08:46

and Y is 0 because both are false now in

play08:50

that case the output would

play08:52

be false right which is zero in this

play08:54

case if x is 0 and Y is 1 in that case

play08:59

you will get again false so you have to

play09:00

make sure both are true

play09:02

then only you will get true and you can

play09:04

see the last one now so when you have

play09:05

the last or when you have 1 and 1 then

play09:08

you will get 1 so if both events are

play09:09

true then only you will get true that's

play09:12

the property of and so the same thing

play09:14

can be done for all we also have odd now

play09:17

this is how your all to table looks like

play09:19

now in all what we have is so we have

play09:21

the same table if you get at least one

play09:23

condition true the resultant condition

play09:25

will also be true right so in this case

play09:27

if I if it is the same thing but not

play09:29

with and with odd you can see the first

play09:32

condition is true second is false in

play09:34

that case you will get true so because

play09:36

at least one is true so both the output

play09:38

will be true so that's how your and and

play09:41

all works we can also use and not here

play09:45

now it is not not basically reverses

play09:47

your output example let's see if I say X

play09:50

is equal to true because that's a

play09:51

resultant thing right okay my bad so if

play09:54

I say X is equal to true so we got a

play09:57

true value right and if I print X we've

play09:59

got true but if I want to negate it what

play10:01

I will do is I will use not X in that

play10:04

case you will say you've got false right

play10:07

so you can remove the operation you can

play10:08

reverse a thing with with the help of

play10:10

not in fact you can say X equal to

play10:12

naught X now so the value of x is false

play10:14

so initially the value of x was true now

play10:16

you are making it false so that's how

play10:18

you can use not now this is a logical

play10:20

operation ok so when you have conditions

play10:22

and if you want to compare two

play10:23

conditions that's where we use logical

play10:25

operators and all or not I can you will

play10:28

be using this a lot in the further

play10:30

videos when we talk about you know

play10:31

different loop patterns we want to plane

play10:33

something and then we will do somehow

play10:35

missing stuff in that amazing stuff

play10:37

we'll be using all these things just to

play10:39

remind that whatever we are doing now is

play10:40

just a foundation for you so that you

play10:42

can work on complex codes right so be

play10:45

with me we will have amazing fun in

play10:47

future videos so I hope you are enjoying

play10:49

this series let me know in the comments

play10:50

section and that's a thanks for watching

play10:52

everyone bye

play11:01

you

Rate This
★
★
★
★
★

5.0 / 5 (0 votes)

Étiquettes Connexes
Python ProgrammingArithmetic OperatorsAssignment ShortcutsRelational ComparisonsLogical ConditionsProgramming TutorialCode FoundationsConditional LogicVariable OperationsEducational Content
Besoin d'un résumé en anglais ?