#11 Python Tutorial for Beginners | Operators in Python
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
π 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.
π 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.
π 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
π‘Assignment Operators
π‘Unary Operator
π‘Relational Operators
π‘Logical Operators
π‘Modulus Operator
π‘Boolean Values
π‘Truth Table
π‘Increment
π‘Negation
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
[Music]
welcome back aliens my name is Ivan
vetti and let's continue with this
series on Python so till this point we
have covered a lot of stuff right we
know how to work with ible now we know
how to perform certain operations and in
this video we'll try to work with
operators now one of some of the
operators we have used white we have
worked with arithmetic operators which
is your addition subtraction division
right now we also have some more
operators which we are going to say in
this video so we'll talk about automatic
operators assignment operators
relational operators and logical
operators and we have one obvious unary
operator so let's start with automatic
in fact we have seen automatic right we
have worked with addition subtraction
they are very simple right so in fact we
have done that before
so I'll be using some variables here
let's say X equal to 2 and Y is equal to
3 so we got 2 variables and down to
perform certain operations and you can
do that right we have done that before
so you can say X plus y you can say X
minus y X multiplied by Y I guess I just
pressed full by mistake and then when
you say X divided by Y even that works
you can see record the output so this is
how you work with automatic operators we
have also seen how to use modulus now
next one is assignment and now you know
like how assignment works we can simply
say X equal to 8 and that's your
assignment operator so any way you use
equal to symbol
that's assignment but then let's try to
do something else what if I say I can
add two numbers like X equal to X plus 2
so what I am doing here is I am adding
the value X itself with 2 so which means
if my x value is let's say 2 plus 2
which is 4 right actually I get that's
what I am expecting here so if I say X
we got 4 now the same operation you can
do in a shortcut way you know we love
shortcuts so here also we have some
shortcuts which is if you want to do a
same thing you can say X I want to
increment okay what is increment
enhancing the values so you will say X
plus I want to increment myself with 2
so you can say X plus equal to 2 so you
are incrementing yourself by 2 and now
if I say X you can see because the value
which is set so which is so X was 4
initially
and then I'm incrementing by two again
so which we got six so instead of saying
X equal to X plus two we can say X plus
equal to two the same thing can be done
with subtraction division multiplication
let's try
so we'll say X multiplied equal to three
so what we are doing is we are
multiplying X by itself so X is six now
and when you say 6 into 3 we got 18 is
that yes that's right okay so this thing
is working you can do short and s but
you can try division and subtraction by
yourself can we do assignment like this
let's say we have two variables here a
and B I want to assign value let's say
five and six to a and B so we can write
in two lines all right we can say a
equal to 5 and B equal to 6 all you can
do that in one line you can say a comma
B is equal to 5 comma 6 so you can do
assignment in one line itself so if I
say enter there is no error if I try to
fetch the value of a which is 5 if you
want to try to try to fetch the value of
B which is 6 so yes you can assign the
values in one line for two variables so
that's about assignment operator now
with this let's move towards the next
operator which is a unary operator
now when I say unity operator it means
one right so we have binary coin set
which is to unity is 1 now how do you
work with unity operator on which one we
can use so let's say we have this
concept of negation right example if I
have a value which is 7 if I were to
negate it so what I will do is I will
use a variable which is n is equal to 7
so we got n as 7 now we can also use so
now we can negate it with the help of
minus n so when you say minus n so if I
if I say the value of NH to the 7 right
so what you can do is if you want to
assign the negative value you can say n
is equal to minus n and if I say enter
and if I print the value of n now which
is minus 7 so you can you know put a
negative sign just by giving a unit
operator which is - so that's your basic
operators assignment and unity the next
two operators which we have is or two
kinds of operators which we have is
logical operators and relational
operators they start with rational first
now what is relational now in fact we
have tons one of the thing before right
in one of this one of the video where we
can compare two numbers or we can
compare two stuff so smaller great
right or compel equal to so let's try
here so there again we'll go for two
variables here which is a and B which we
have done before and of course the value
of a is five and the value of B is six
so can I do this I want to check if a is
less than B so you can say a is less
than B and answer is yes right because a
is 5 and B is 6 so when I say enter now
it says true and if you remember we have
done that in boolean so it returns our
true and false so if we when you use
this angular symbol it's less than we
can also use greater than symbol right
now what if the value of a and B of
course they are not same so how do you
check for these say comparison if they
are same in because if we cannot use
single or single equal to because single
equal to means you are assigning it we
are not assigning here we are comparing
it so in that case you will be using
double equal to and then you will say B
and you can see the got false and of
course right a is 5 and B is 6 so of
course it will give you false now in
that case what you will do is you will
say let's change the value of a I will
say a is also 6
so now B is 6 a is 6 let's perform the
same same operation I will say a is
equal to equal to B and you can see we
got true now that same light so it will
give you true I want to check if a is
less than or equal to B can they do both
the same time less than and equal to yes
we can so we can say a less than equal
to B so we can use this symbol in fact
in maths we also have a symbol right so
the same way we have in programming we
can just put less than and equal to the
same thing will be done for greater than
equal to of course it's not greater
unless it's equal to so it will compare
either less or equal to and you can see
it works now we can also also check if
they are not equal now how do we check
not equal let's say something else and
be something else and I want to check if
there are if they're not equal in that
case you will use not equal to B so not
is explanation so in programming
remember this thing whenever you see
exclamation that's that's are not simple
so we got a not equal to B if I say
enter you can see we got false why false
is because your a is equal to P right so
when you say not equal to it it doesn't
make sense it it is false but yes the
value of a or b is let's say
7.now so we have a and B with different
values if I perform the same operation a
not equal to B you can see record true
right so this are called as relational
because you are trying to relate to
different values the next set we have is
logical operators
so before understanding what logical
operators are you to understand the
concept of true and false and relation
between so if you have two conditions
and if you want to come and if you want
to combine those two conditions now it
can be based on and or it can be based
on all or it can be not so basically we
have three operators here and all and
not now let's say we have two conditions
one is related to a or we can say if a
let me explain this shell once so let's
again start with variables we can say
here we got two variables will say a is
equal to 5 and B is equal to let's say 4
we got 2 variables okay and individually
I want to check their preference example
if a is less than 8 and I also want to
check B should not be greater than 5 how
do I check both and I want to make sure
that a should be less than 8 and B
should be less than 5
in that case you need to rewrite two
conditions one is a less than 8 and B is
less than 5 so we have two conditions
here what should I put in between so in
that case you can use a logical
operators which is and so I want to
compare I want to check both right so I
would say and and you can see we got
true because both are true now what if I
say I want to check if a is less than 8
and B is less than 2 what do you think
what is the output is it true or false
see now the first condition is true
right
and second is false because B is not
less than 2 in that case you will think
okay one exploit should give you true
but unfortunately get false
now why it's the case because for to
understand that we have a table here now
this table is also called a truth table
you can represent true with one and
Falls with zero this is how it looks
like so when you have let's say we have
x and y so X represents one condition
output and while present second an
output will compare this so if X is zero
and Y is 0 because both are false now in
that case the output would
be false right which is zero in this
case if x is 0 and Y is 1 in that case
you will get again false so you have to
make sure both are true
then only you will get true and you can
see the last one now so when you have
the last or when you have 1 and 1 then
you will get 1 so if both events are
true then only you will get true that's
the property of and so the same thing
can be done for all we also have odd now
this is how your all to table looks like
now in all what we have is so we have
the same table if you get at least one
condition true the resultant condition
will also be true right so in this case
if I if it is the same thing but not
with and with odd you can see the first
condition is true second is false in
that case you will get true so because
at least one is true so both the output
will be true so that's how your and and
all works we can also use and not here
now it is not not basically reverses
your output example let's see if I say X
is equal to true because that's a
resultant thing right okay my bad so if
I say X is equal to true so we got a
true value right and if I print X we've
got true but if I want to negate it what
I will do is I will use not X in that
case you will say you've got false right
so you can remove the operation you can
reverse a thing with with the help of
not in fact you can say X equal to
naught X now so the value of x is false
so initially the value of x was true now
you are making it false so that's how
you can use not now this is a logical
operation ok so when you have conditions
and if you want to compare two
conditions that's where we use logical
operators and all or not I can you will
be using this a lot in the further
videos when we talk about you know
different loop patterns we want to plane
something and then we will do somehow
missing stuff in that amazing stuff
we'll be using all these things just to
remind that whatever we are doing now is
just a foundation for you so that you
can work on complex codes right so be
with me we will have amazing fun in
future videos so I hope you are enjoying
this series let me know in the comments
section and that's a thanks for watching
everyone bye
you
Browse More Related Video
43. OCR A Level (H046-H446) SLR8 - 1.2 Introduction to programming part 4 mathematical operators
Data Types Variables And Operators In Python | Python Fundamentals | Great Learning
P_15 Operators in Python | Assignment and Comparison Operators | Python Tutorials for Beginners
Operators
Operators & Operands | Godot GDScript Tutorial | Ep 02
41. OCR A Level (H046-H446) SLR8 - 1.2 Introduction to programming part 2 variables & constants
5.0 / 5 (0 votes)