Posts

Showing posts from February, 2026

Python Function

 Python Function- Write Once, Use Many Times Introduction Imagine you need to calculate the sum of two numbers many times in your programs with different values. Will you write the code again and again? No. Instead, you create a function in a program, that can have arguments, if needed can perform certain functionality can return a result What is a Function? A function is a block of code that performs a specific task. In Python we create functions using the def keyword. Basic Function Syntax def function_name(): #code Example: def calcSomething (x):     r=2* x**2     return r a= int(input("Enter a number:")) print(calcSomething(a)) 1. Where x is the argument to function calcSomething 2. return statement returns the computed result. 3. calcSomething(a) calls the function. Calling/Invoking/Using a Function To use a function that has been defined earlier, you need to write a function call statement in Python. A function call statement takes the following form: ...

Python loops (for loop & while loop)

 Python Loops(for loop & while loop) Introduction Imagine you want to print "Hello, world!" 5 times, then in Python, you will simply write print("Hello, world ") 5 times. But what if you want to print it 100 times? It's not easy, and also it waste lot of your time. So to solve this problem, loops are used. Loops help us repeat a block of code multiple times. What Is a Loop? A loop is used to execute a block of code again and again until a condition is met. Python has two main types of loops: for loop while loop The for Loop A for loop is used when we know how many times we want to repeat the code or a statement. The syntax of the code is as follows: code 1: #print numbers from 1 to 5 for i in range(1,6):     print(i) output: 1 2 3 4 5 Understanding range() range(start, stop) means: start from this number stop before the last number (stop - 1) e.g. range(1,6) will print the value from 1 to 5 code 2: #print Hello 5 times for i in range(5):     print("He...

Python Conditional Statements(if, elif, else)

Image
 Python Conditional Statements(if, elif, else)   Introduction In real life, we take decision every day like if rain then take an umbrella, if phone battery is low then charge it, etc. Similarly, in python, conditional statement are used for decision making. In this blog, you'll learn: What conditional statements are if, elif and else What Are Conditional statements? Conditional statements allow a program to perform different action based on whether a certain condition is true or false.  Suppose if we have to find the given number is divisible from 2 or not. Python mainly uses : if  elif else   Let's take above example: Code: #find numbers divisible by two and which are not. b=13 if b%2==0:     print("b is divisible by 2") else:     print("b is not divisible by 2") Output: b is not divisible by 2 The if  Statement The if statement runs code only when the condition is true. if statement can not be written without condition....

Python Input and Output(input() & print())

 Python Input and Output(input() & print()) Introduction Just like we need clear instructions in real life to complete a task, Python programs need input from users to work correctly. In Python, we take input using the input() function , and after processing that input, we display the result as output using the print() function . In this blog, you'll learn Python Input and Output with easy examples. What is Output in Python?  Output means showing something on the screen. Python uses the print() function to display output. Example: print("Hello, world!") Output: Hello, world! Printing Multiple Values You can print multiple values in one line. Code: name = "Sundeep" age = 21 Output: Sundeep 21 Print Text with Variable Code: name = "Python" print("I am learning", name) Output: I am learning Python What is Input in Python? Input means taking data from the user. Python uses the input() function to take user input. Example: name = input(...

Python operators

Image
Python Operators  Introduction After learning Python variables and data types , the next important topic is Python Operators . Operators are used to perform operations on values and variables. In this blog, we'll learn about: What are operators ? Types of operators in Python These topics are important for problem-solving , coding and interviews. What are Operators? Python can perform calculations using operators. Python provides types of operators that can manipulate and work with different data types. You can also perform normal operation in the IDLE shell .  Types of operations Python have different types of operations: Arithmetic Operators Comparison Operators Logical Operators Assignment Operators Membership Operators Identity Operators 1. Arithmetic Operators Used to perform mathematical calculations on numeric values. Addition (+): Addition is used for adding two operands in the simplest form of expression, like op1 operator op2. e.g. 5+2=7 or 7+5=12 Subtraction (-): Su...