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

WP: 8. Dynamically show post taxonomies and their data in tab format [Solved]

    Today I ran into a problem, I need to dynamically show post taxonomies and their data in tab format and found this solution. And nonetheless, It's better to fix the thing with some bunch of code rather than uploading one more bulky plugin on your website that will end up making your site heavy. Just follow the steps below and avoid heading up into any malicious or bulky plugin for this particular problem from now on forever. Step 1: Get the taxonomies terms first <?php     $terms = get_terms( array(       'taxonomy' => 'off_plan_categories',       'hide_empty' => true,     ) );     // echo '<pre>';     // print_r($terms);     // die; ?> Step 2:  Execute loop first for tab navigation <nav id="offPlanPropertyTabs" class="offplan-property-type-tabs nav nav-tabs"> <?php foreach($terms as $key => $term): ?>   <button class="nav-item nav-link <?php if($key==0): ?> active <?php en

JS: Cheatsheet

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

C++: 9. File Handling

Files are used to store data in a storage device permanently. File handling provides a mechanism to store the output of a program in a file and perform various operations on it. Example of opening/creating a file    #include<iostream> #include<fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file.close(); // Step 4: Closing file } return 0; } Writing to a File #include <iostream> #include <fstream> using namespace std; int main() { fstream new_file; new_file.open("new_file_write.txt",ios::out); if(!new_file) { cout<<"File creation failed"; } else { cout<<"New file created"; new_file<<"Learning File handling"; //Writing to file new_file.close();