python examples 3 كورس تعلم بايثون من الصفر

python  examples 3 كورس تعلم بايثون من الصفر  تعلم بايثون للمبتدى والمحترف ، دوره شاملة بايثون ، تمارين بايثون  تعلم لغة بايثون .




Write a Python program to add two objects if both objects are an integer type.

Write a Python program to add two objects if both objects are an integer type.

Example

Test.py
						

# Code:

def add_numbers(a, b):
if not (isinstance(a, int) and isinstance(b, int)):
raise TypeError("Inputs must be integers")
return a + b

print(add_numbers(10, 20))


					  

Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).

Write a Python program to compute the distance between the points (x1, y1) and (x2, y2).

Example

Test.py
						

#Code :

import math
p1 = [4, 0]
p2 = [6, 6]
distance = math.sqrt( ((p1[0]-p2[0])**2)+((p1[1]-p2[1])**2) )

print(distance)


					  

Write a Python program to access and print a URL's content to the console.

Write a Python program to access and print a URL's content to the console.

Example

Test.py
						
# Code:

from http.client import HTTPConnection
conn = HTTPConnection("example.com")
conn.request("GET", "/")  
result = conn.getresponse()
# retrieves the entire contents.  
contents = result.read() 
print(contents)


					  

Write a Python program to check if a number is positive, negative or zero.

Write a Python program to check if a number is positive, negative or zero.

Example

Test.py
						

# Code:

num = float(input("Input a number: "))
if num > 0:
print("It is positive number")
elif num == 0:
print("It is Zero")
else:
print("It is a negative number")


					  

Write a Python program to determine the largest and smallest integers, longs, floats.

Write a Python program to determine the largest and smallest integers, longs, floats.

Example

Test.py
						
 
# Code:

import sys
print("Float value information: ",sys.float_info)
print("\nInteger value information: ",sys.int_info)
print("\nMaximum size of an integer: ",sys.maxsize)


					  

Write a Python program to check whether an integer fits in 64 bits.

Write a Python program to check whether an integer fits in 64 bits.

Example

Test.py
						

# Code:

int_val = 30
if int_val.bit_length() <= 63:
print((-2  63).bit_length())
print((2  63).bit_length())


					  

Write a Python program to check whether multiple variables have the same value.

Write a Python program to check whether multiple variables have the same value.

Example

Test.py
						

# Code:

x = 20
y = 20
z = 20
if x == y == z == 20:
print("All variables have same value!")


					  

Write a Python program to check whether a variable is integer or string.

Write a Python program to check whether a variable is integer or string.

Example

Test.py
						

# Code :

print(isinstance(25,int) or isinstance(25,str))
print(isinstance([25],int) or isinstance([25],str))
print(isinstance("25",int) or isinstance("25",str))


					  

Write a Python program to test if a variable is a list or tuple or a set.

Write a Python program to test if a variable is a list or tuple or a set.

Example

Test.py
						

# Code :

#x = ['a', 'b', 'c', 'd']
#x = {'a', 'b', 'c', 'd'}
x = ('tuple', False, 3.2, 1)
if type(x) is list:
print('x is a list')
elif type(x) is set:
print('x is a set')
elif type(x) is tuple:
print('x is a tuple')    
else:
print('Neither a list or a set or a tuple.')


					  

Write a Python function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number.

Write a Python function that takes a positive integer and returns the sum of the cube of all the positive integers smaller than the specified number.

Example

Test.py
						

# Ex.: 8 = 73+63+53+43+33+23+13 = 784

Code:

def sum_of_cubes(n):
n -= 1
total = 0
while n > 0:
total += n * n * n
n -= 1
return total
print("Sum of cubes: ",sum_of_cubes(3))


					  
تعليقات