Skip to main content

Py: 2. Python Fundamentals


 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:-

  1. Number (Int, Float)
  2. Sting
  3. 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 −
+ AdditionAdds values on either side of the operator.a + b = 30
- SubtractionSubtracts right hand operand from left hand operand.a – b = -10
* MultiplicationMultiplies values on either side of the operatora * b = 200
/ DivisionDivides left hand operand by right hand operandb / a = 2
% ModulusDivides left hand operand by right hand operand and returns remainderb % a = 0

Comparison Operators:-

Assume variable a holds 10 and variable b holds 20, then −

OperatorDescriptionExample
==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.

Assignment Operators:- 
Assume variable a holds 10 and variable b holds 20, then −
OperatorDescriptionExample
=Assigns values from right side operands to left side operandc = a + b assigns value of a + b into c
+= Add ANDIt adds right operand to the left operand and assign the result to left operandc += a is equivalent to c = c + a
-= Subtract ANDIt subtracts right operand from the left operand and assign the result to left operandc -= a is equivalent to c = c - a
*= Multiply ANDIt multiplies right operand with the left operand and assign the result to left operandc *= a is equivalent to c = c * a
/= Divide ANDIt divides left operand with the right operand and assign the result to left operandc /= a is equivalent to c = c / a
%= Modulus ANDIt takes modulus using two operands and assign the result to left operandc %= a is equivalent to c = c % a
User Input:-
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

Popular posts from this blog

JS: Cheatsheet

   Datatypes Difference Between Var, Let and Const Functions and its types Asynchronous operations in JS

C++: 7. Inheritance

In C++, inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which are defined in other class. In C++, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class. Single Level Inheritance When one class inherits another class, it is known as single level inheritance. Example:-  #include <iostream>   using namespace std;    class Account {      public:      float salary = 60000;     };      class Programmer: public Account {      public:      float bonus = 5000;        };        int main(void) {   ...