Home Essential Python Concepts and Functions

Essential Python Concepts and Functions

Print in Python Without New Line

By default, Python’s print() function prints output followed by a newline. To print without a newline, use the end parameter:

print("Hello", end=" ")

Output:

Hello World

Basic Mathematical Operations

print(1 + 3)   # Addition
print(1 * 3)   # Multiplication
print(3 ** 2)  # Exponentiation
print(pow(3, 2)) # Using pow()

Divide in Python

print(5 / 2)  # Floating-point division (2.5)
print(5 // 2) # Floor division (2)
print(5 % 2)  # Modulus (1)

Relational Operations

print(45 > 20)   # True
print(23 == 34)  # False
print(32 <= 32)  # True
print(24 != 45)  # True

Logical Operations

print(3 > 2 and 3 != 4)  # True
print(3 == 4 and 5 < 7)  # False
print(3 == 4 or 5 < 7)   # True
print(not 3 == 4)        # True

Primitive Data Types

print(type(2))        # <class 'int'>
print(type(3.123))    # <class 'float'>
print(type("Dog"))    # <class 'str'>
print(type(True))     # <class 'bool'>
print(type(False))    # <class 'bool'>

Checking Value Type

print(isinstance(2, int))       # True
print(isinstance(3.455, float)) # True
print(isinstance(3.455, int))   # False

Type Casting

print(int(3.212))  # 3
print(float(3))    # 3.0
print(int("45"))   # 45

Python Variables

x = 20
y = 30
print(x + y)

z = x - y
print(z)

s = x > 30
print(s)

p = "Today is a beautiful day!!"
print(p)

Special Features of Python Variables

a = b = c = 10
print(a, b, c)

p, q = 10, 20
print(p, q)

User Inputs in Python

name = input("Please enter your name: ")
print(name)

age = input("Please enter your age: ")
print(age)
print(type(age))

age_2years = int(age) + 2
print(age_2years)

num1 = float(input("Enter the first value:"))
num2 = float(input("Enter the second value:"))
num3 = num1 + num2
print(num3)

Python Comments

# This is a single-line comment
num1 = 20
num2 = 30
print(num1 + num2)

'''
This is a multi-line comment
or a docstring.
'''
print(25 + 40)

Using find_all in Python

The find_all() method is commonly used in web scraping with BeautifulSoup to extract all matching elements:

from bs4 import BeautifulSoup
html = "<div><p>Hello</p><p>World</p></div>"
soup = BeautifulSoup(html, "html.parser")
print(soup.find_all("p"))

 

 

 

Comments are closed.