Python Codes Basic to Advance
前往频道在 Telegram
Python Codes Basic to Advance All Codes @C_Codes_pro @CPP_Codes_pro @Java_Codes_Pro @nodejs_codes_pro Discussion @bca_mca_btech
显示更多2 813
订阅者
无数据24 小时
-47 天
-2430 天
帖子存档
# Some more examples of functions
# under """ these three inverted commas are string which is used as comment in Python
def greet(name):
"""
This function takes a name as input and prints a greeting message.
"""
print(f"Hello, {name}!")
def multiply(x, y):
"""
This function takes two numbers as input and returns their product.
"""
return x * y
def is_even(number):
"""
This function checks if a given number is even and returns a boolean value.
"""
return number % 2 == 0
# Calling the functions
greet("Alice") # Output: Hello, Alice!
product = multiply(4, 5)
print(product) # Output: 20
print(is_even(7)) # Output: False
print(is_even(10)) # Output: True
/py # Some more examples of functions
# under """ these three inverted commas are string which is used as comment in Python
def greet(name):
"""
This function takes a name as input and prints a greeting message.
"""
print(f"Hello, {name}!")
def multiply(x, y):
"""
This function takes two numbers as input and returns their product.
"""
return x * y
def is_even(number):
"""
This function checks if a given number is even and returns a boolean value.
"""
return number % 2 == 0
# Calling the functions
greet("Alice") # Output: Hello, Alice!
product = multiply(4, 5)
print(product) # Output: 20
print(is_even(7)) # Output: False
print(is_even(10)) # Output: True
# Some more examples of functions
# under """ these three inverted commas are string which is used as comment in Python
def greet(name):
"""
This function takes a name as input and prints a greeting message.
"""
print(f"Hello, {name}!")
def multiply(x, y):
"""
This function takes two numbers as input and returns their product.
"""
return x * y
def is_even(number):
"""
This function checks if a given number is even and returns a boolean value.
"""
return number % 2 == 0
# Calling the functions
greet("Alice") # Output: Hello, Alice!
product = multiply(4, 5)
print(product) # Output: 20
print(is_even(7)) # Output: False
print(is_even(10)) # Output: True
# function declaration in Python
def sum:
return 6 + 7
print(sum())
/py # function declaration in Python
def sum:
return 6 + 7
print(sum())
import urllib.request
url = 'https://song.panditsiddharth.repl.co/lyrics?song=har+har+shambhu'
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
print(data)
/py import urllib.request
url = 'https://song.panditsiddharth.repl.co/lyrics?song=har+har+shambhu'
response = urllib.request.urlopen(url)
data = response.read().decode('utf-8')
print(data)
All Codes
@C_Code5
@CPP_Coding
@Python_Codes_Pro
@Java_Codes_Pro
@jsCode0
Diffrent free Compiler bots
For group @CodeCompiler_Bot
For channel @IOChannel_bot
For Channel Cmpl @Compiler0bot
For inline @cmpbbot
info in @LogicBots
Discussion
@bca_mca_btech
# Dimond pattern
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
print(end=" ")
for j in range(1, 2 * i):
print("*", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows - i + 1):
print(end=" ")
for j in range(1, 2 * i):
print("*", end="")
print()
# @python_Codes_pro
/py
# Dimond pattern
rows = 5
for i in range(1, rows + 1):
for j in range(1, rows - i + 1):
print(end=" ")
for j in range(1, 2 * i):
print("*", end="")
print()
for i in range(rows - 1, 0, -1):
for j in range(1, rows - i + 1):
print(end=" ")
for j in range(1, 2 * i):
print("*", end="")
print()
# @python_Codes_pro# Star Pyramid pattern
rows = 5
for i in range(0, rows):
for j in range(0, rows - i - 1):
print(end=" ")
for j in range(0, i + 1):
print("*", end=" ")
print()
# @python_Codes_pro
/py
# Star Pyramid pattern
rows = 5
for i in range(0, rows):
for j in range(0, rows - i - 1):
print(end=" ")
for j in range(0, i + 1):
print("*", end=" ")
print()
# @python_Codes_pro
# Number triangle pattern
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
# @python_Codes_pro
/py # Number triangle pattern
rows = 5
for i in range(1, rows + 1):
for j in range(1, i + 1):
print(j, end=" ")
print()
# @python_Codes_pro
# Creating a List
my_list = []
print("Empty list:", my_list)
my_list = [1, 2, 3, 4, 5]
print("List with initial values:", my_list)
# Accessing List Elements
element = my_list[0]
print("Element at index 0:", element)
last_element = my_list[-1]
print("Last element:", last_element)
# Modifying List Elements
my_list[2] = 10
print("List after updating element at index 2:", my_list)
# Adding Elements to the List
my_list.append(6)
print("List after appending element:", my_list)
my_list.insert(3, 7)
print("List after inserting element at index 3:", my_list)
another_list = [8, 9, 10]
my_list.extend(another_list)
print("List after extending with another list:", my_list)
# Removing Elements from the List
my_list.remove(4)
print("List after removing element 4:", my_list)
removed_element = my_list.pop(2)
print("List after removing element at index 2:", my_list)
print("Removed element:", removed_element)
my_list.clear()
print("List after clearing all elements:", my_list)
# Slicing a List
sliced_list = my_list[1:3]
print("Sliced list from index 1 to 3 (exclusive):", sliced_list)
sub_list = my_list[2:]
print("Sublist from index 2 till the end:", sub_list)
reversed_list = my_list[::-1]
print("Reversed list:", reversed_list)
# List Iteration
for element in my_list:
print("Element:", element)
for index, element in enumerate(my_list):
print("Index:", index, "Element:", element)
/py
# Creating a List
my_list = []
print("Empty list:", my_list)
my_list = [1, 2, 3, 4, 5]
print("List with initial values:", my_list)
# Accessing List Elements
element = my_list[0]
print("Element at index 0:", element)
last_element = my_list[-1]
print("Last element:", last_element)
# Modifying List Elements
my_list[2] = 10
print("List after updating element at index 2:", my_list)
# Adding Elements to the List
my_list.append(6)
print("List after appending element:", my_list)
my_list.insert(3, 7)
print("List after inserting element at index 3:", my_list)
another_list = [8, 9, 10]
my_list.extend(another_list)
print("List after extending with another list:", my_list)
# Removing Elements from the List
my_list.remove(4)
print("List after removing element 4:", my_list)
removed_element = my_list.pop(2)
print("List after removing element at index 2:", my_list)
print("Removed element:", removed_element)
my_list.clear()
print("List after clearing all elements:", my_list)
# Slicing a List
sliced_list = my_list[1:3]
print("Sliced list from index 1 to 3 (exclusive):", sliced_list)
sub_list = my_list[2:]
print("Sublist from index 2 till the end:", sub_list)
reversed_list = my_list[::-1]
print("Reversed list:", reversed_list)
# List Iteration
for element in my_list:
print("Element:", element)
for index, element in enumerate(my_list):
print("Index:", index, "Element:", element)#4 Arithmeitic Operations
var1 = 9 #Variable1
var2 = 3 #Variable2
#Adding
print (var1, '+', var2, '=', var1 + var2)
#Subtracting
print (var1, '-', var2, '=', var1 - var2)
#Multiplying
print (var1, '×', var2, '=', var1 * var2)
#Dividing
print (var1, '÷', var2, '=', var1 / var2)
/py #4 Arithmeitic Operations
var1 = 9 #Variable1
var2 = 3 #Variable2
#Adding
print (var1, '+', var2, '=', var1 + var2)
#Subtracting
print (var1, '-', var2, '=', var1 - var2)
#Multiplying
print (var1, '×', var2, '=', var1 * var2)
#Dividing
print (var1, '÷', var2, '=', var1 / var2)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
intersection_set = set1.intersection(set2)
print(intersection_set)
