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

React: Print Specific Section of the page with - ReactToPrint

  Want to know how can you easily print specific section of your website just follow the steps below and I am sure you will find this pretty easy. There is no rocket science to crack it you just need basic code knowledge only. Step 1: Add the ReactToPrint in your project    npm install --save react-to-print Step2: Add the following code in your component   import React, { useRef } from 'react'; import ReactToPrint from 'react-to-print'; const Example = () => {      const componentRef = useRef();      return (           <div>                <ReactToPrint                     trigger={() => <button>Print this out!</button>}                     content={() => componentRef}                />                <table ref={componentRef}>                     <tr>                         <td>Printable data goes here</td>                     </tr>               </table>     

JS: Change Node version on windows

  You may need to change your current node version to some old or new version for the compatibility of a project someday, So here is a guide to do so on windows PC, let's just check it out.  Step 1: Visit this link  https://github.com/coreybutler/nvm-windows Step2: Just click on  latest installer link in the bottom Step3: Download the  nvm-setup.zip from here and install the setup. Step4: After the installation open cmd with administrative mode . Step5: Execute the command nvm install the_version_you_want Step6: Now nvm use  the_version_you_want Step6: Hurray the version is changed you can check by this command nvm list Now Cheers !😊