Variable:-
Variable is a name that is used to refer to memory location. In Python, we don't need to specify the type of variable because Python is smart enough to get variable type. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_).
Syntax:-
num= 345
name= "mohit"
Variable Types:-
- Number (Int, Float)
- Sting
- Boolean
Get the Type of the variable:-
x = 5
y = "John"
print(type(x))
print(type(y))
Output:-
<class 'int'>
<class 'str'>
Operators:-
The operator can be defined as a symbol which is responsible for a particular operation between two operands. Python provides a variety of operators, which are described as follows.
Arithmetic Operators:-
Assume variable a holds 10 and variable b holds 20, then −
+ Addition | Adds values on either side of the operator. | a + b = 30 |
- Subtraction | Subtracts right hand operand from left hand operand. | a – b = -10 |
* Multiplication | Multiplies values on either side of the operator | a * b = 200 |
/ Division | Divides left hand operand by right hand operand | b / a = 2 |
% Modulus | Divides left hand operand by right hand operand and returns remainder | b % a = 0 |
Comparison Operators:-
Assume variable a holds 10 and variable b holds 20, then −
Operator | Description | Example |
---|---|---|
== | If the values of two operands are equal, then the condition becomes true. | (a == b) is not true. |
!= | If values of two operands are not equal, then condition becomes true. | (a != b) is true. |
> | If the value of left operand is greater than the value of right operand, then condition becomes true. | (a > b) is not true. |
< | If the value of left operand is less than the value of right operand, then condition becomes true. | (a < b) is true. |
>= | If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. | (a >= b) is not true. |
<= | If the value of left operand is less than or equal to the value of right operand, then condition becomes true. | (a <= b) is true. |
Assume variable a holds 10 and variable b holds 20, then −
Operator | Description | Example |
---|---|---|
= | Assigns values from right side operands to left side operand | c = a + b assigns value of a + b into c |
+= Add AND | It adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
-= Subtract AND | It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c - a |
*= Multiply AND | It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
/= Divide AND | It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / a |
%= Modulus AND | It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
input() function is used to get data from the user while working with the python. The function input continues to read input text from the user until it encounters a new line.
Example:-
name = input("Enter your name: ")
print('Welcome', name)
Output:-
Welcome Mohit
Functions:-
Functions are the most important aspect of an application. A function can be defined as the organized block of reusable code, which can be called whenever required.
Syntax:-
def my_function(parameters):
function_block
return expression
Example:-
# Defining function
def sum():
a = 10
b = 20
c = a+b
return c
# calling sum() function in print statement
print("The sum is:",sum())
Output:-
The sum is: 30
Identation:-
Since Python Functions don't have any explicit beginning or end, like curly braces, to indicate the start and stop for the function; they have to rely on the indentation only.
Comments:-
We use # symbol to define a comment in python
Comments
Post a Comment