Python Test For Cyberyaan

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

#Q1.

# #integer
# my_integer = 42
#
# #float
# my_float = 3.14
#
# #string
# my_string = "Hello, World!"
#
# # Boolean
# my_boolean = True
#
# #list
# my_list = [1, 2, 3, 4, 5]
#
# #tuple
# my_tuple = (1, 2, 3, 4, 5)
#
# #dict
# my_dict = {'name': 'John Doe', 'age': 30, 'city': 'New York'}
#
# #set
# my_set = {1, 2, 3, 4, 5}
#
# # complex
# my_complex = 1 + 2j

#Q3.
#exm:- list my_list = [1, 2, 3, 4, 5] # A list of integers

#exm:- tuple my_tuple = (1, 2, 3, 4, 5) # A tuple of integers

#exm:- set my_set = {1, 2, 3, 4, 5} # A set of integers

#exm:- dictionary my_dict =


# { "name": "John",
# "age": 30,
# "city": "New York"}

#Q4.
#if (condition):
# Code to execute if the condition is true
#else:
# Code to execute if the condition is false

#Q5.

# A while loop is used when you want to execute a block of code as long as a
certain condition is True.
# It's useful when you don't know in advance how many times the code should run.
#
#while condition:
# Code to execute as long as the condition is True

# import random
#
# while True:
# roll = random.randint(1, 6)
# print("Rolled:", roll)
# if roll == 6:
# break # Exit the loop when a 6 is rolled

#Q6.
# Scope:
#
# Local Varia:
#
# A local variable is one that is defined within a specific block or function.
# It is only accessible within the block or function where it is defined.
# Outside of this block or function, the variable is not visible and cannot be
used.
# Global Variable:
#
# A global variable is one that is defined outside of any function or block.
# It is accessible throughout the entire program, including within functions
#
# Visibility:
#
# Local Variable:
#
# A local variable is only visible and accessible within the specific block or
function where it is defined.
# It cannot be used outside of this context.
# Global Variable:
#
# A global variable can be accessed from anywhere in the program, including inside
functions.
# Lifetime:
#
# Local Variable:
#
# The lifetime of a local variable is limited to the duration of the block or
function in which it is defined.
# Once the block or function finishes executing, the local variable goes out of
scope and is destroyed.
# Global Variable:
#
# The lifetime of a global variable is from the point it is defined until the end
of the program's execution.
# It remains in memory for the entire duration of the program.
# Name Conflicts:
#
# Local Variable:
#
# A local variable can have the same name as a global variable or another local
variable in a different scope.
# In such cases, the local variable takes precedence within its scope.
# Global Variable:
#
# If a global variable and a local variable have the same name, the local variable
takes precedence within its scope.
# Modifiability:
#
# Local Variable:
#
# A local variable can only be modified within the block or function where it is
defined.
# Changes made to a local variable do not affect the global variable with the same
name.
# Global Variable:
#
# A global variable can be modified anywhere in the program, including inside
functions.
# Changes made to a global variable are reflected throughout the program.
# Overhead:
#
# Local Variable:
#
# Local variables are generally more memory efficient because they only exist
within the scope of a block or function.
# Global Variable:
#
# Global variables can potentially consume more memory, especially if they store
large amounts of data, as they persist throughout the program's execution.
# In summary, local variables are confined to a specific block or function and have
limited scope, while global variables are accessible anywhere in the program and
have broader scope.
# It's important to use variables with consideration for scope and to avoid naming
conflicts. Additionally, global variables should be used judiciously to maintain
code readability and organization.

#Q7.

# def sum_numbers():
# total = 0
# while True:
# try:
# user_input = float(input("Enter a number (or 0 to exit): "))
# if user_input == 0:
# break
# total += user_input
# except ValueError:
# print("Invalid input. Please enter a number or 0 to exit.")
# return total
#
# result = sum_numbers()
# print(f"The sum of the numbers entered is: {result}")

#Q8.
# def multiplication_table(number, range_limit):
# for i in range(1, range_limit + 1):
# result = number * i
# print(f"{number} x {i} = {result}")
#
#
# multiplication_table(5, 10)

#Q9.
# def sum_list_values(lst):
# total = 0
# for item in lst:
# total += item
# return total
#
# # Example usage:
# l = [2, 3, 3, 4, 5, 33, 5, 5, 5, 5, 5]
# result = sum_list_values(l)
# print(f"The sum of the list values is: {result}")

#Q10.
# user1 = float(input("Enter the first number: "))
# user2 = float(input("Enter the second number: "))
# user3 = float(input("Enter the third number: "))
#
#
# maximum = max(user1, user2, user3)
#
#
# print(f"The maximum number is: {maximum}")

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy