If-Else Statements in Python

Basic If Statement The simplest form of decision making in Python. The if statement runs a block of code only when the given condition is true. age = 18 if age >= 18: print(“You are eligible to vote.”) Output: You are eligible to vote. If-Else Statement Use else to specify a block of code that…

Python Variables, Data Types, Scope, and Type Casting

Python is a dynamically typed language, which means you don’t need to declare the type of variable before using it. This article will walk you through some fundamental concepts such as variable declaration, data types, f-strings, type casting, and the scope of variables. Variable Declaration and f-String Formatting You can store different types of values…

Print function and String manipulation in python

Basic Print Statements Demonstrates printing of various data types and multiple items in one statement. Output:Hello54.12Hi 2 5.7 Are you good?Hi, therePrint single inverted comma ‘ within a string by surrounding the whole string with double inverted comma f-String Example Shows how to embed variables directly inside strings using f-strings for clearer and cleaner formatting….

Python-Matrix Addition

This code defines a function mat_add that adds three matrices A, B, and C. def mat_add(A,B,C): if(len(A)==len(B) and (len(B)==len(C)) and (len(A[0])==len(B[0])) and (len(B[0])==len(C[0]))): for i in range(len(A)): for j in range(len(A[0])): A[i][j]=A[i][j]+B[i][j]+C[i][j] return A else: return -1 A=[[1,2,3],[1,2,3],[2,4,6]] B=[[1,2,3],[1,2,3],[2,4,6]] C=[[1,2,3],[1,2,3],[2,4,7]] x=mat_add(A,B,C) print(x) Here’s a line-by-line explanation of the function : def mat_add(A, B, C):This line…

Python-Prime Check and Prime Count

prime check: The below programs will show you how to check a given number is prime or not and count the number of primes in a list. The function is_prime(n) determines whether a given number n is a prime number. It first checks if n is less than 2; if so, the function returns False…