0% found this document useful (0 votes)
12 views

Python Basic

past question

Uploaded by

musfiqul.showmik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python Basic

past question

Uploaded by

musfiqul.showmik
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

STEM HERO BD

# Integer # Boolean # Dictionary


x = 10 is_valid = True person = {"name": "Alice",
print(f"Integer: {x}") print(f"Boolean: {is_valid}") "age": 25}
print(f"Dictionary: {person}")
# Floating Point Number # List
y = 10.5 fruits = ["apple", "banana", # Set
print(f"Float: {y}") "cherry"] unique_numbers = {1, 2, 3, 4}
print(f"List: {fruits}") print(f"Set:
# String {unique_numbers}")
name = "Alice" # Tuple
print(f"String: {name}") coordinates = (10.0, 20.0) # NoneType
print(f"Tuple: {coordinates}") value = None
print(f"NoneType: {value}")

Variable:

Difference:
Summary of Key Differences
Feature List Tuple Dictionary Set
Ordered Yes Yes No No
Mutable Yes No Yes Yes
Keys: No,
Duplicates Allowed Allowed Not allowed
Values: Yes
Syntax [] () {key: value} {} or set()
Indexing By index By index By key No indexing
Ordered collection Ordered collection that Key-value Unique items collection
Use Case
that can change should not change pairs and set operations

Print:
Instructor : Musfiqul Hamid Showmik
STEM HERO BD

# Basic Usage # Using `format()` Method # Printing Data Structures


print("Hello, World!") print("Name: {}, Age:
{}".format(name, age)) # Printing Lists
# Printing Variables fruits = ["apple", "banana",
name = "Alice" # Using f-Strings (Python 3.6+) "cherry"]
age = 25 print(f"Name: {name}, Age: print(fruits)
print(name) {age}")
print(age) # Printing Tuples
# Printing with Custom coordinates = (10.0, 20.0)
# Formatting Output Separators and End Characters print(coordinates)

# String Concatenation # Custom Separator # Printing Dictionaries


print("Hello, " + name + "!") print("apple", "banana", person = {"name": "Alice",
"cherry", sep=", ") "age": 25}
# Comma-Separated Values print(person)
print("Name:", name, "Age:", # Custom End Character
age) print("Hello, ", end="")
print("World!")
# Multi-Line Printing
# Printing Sets # Using `format()` Method # Triple Quotes
unique_numbers = {1, 2, 3, 4} with Positional and Keyword print("""This is a
print(unique_numbers) Arguments multi-line
# Printing with Formatting print("Name: {0}, Age: string""")
# Using `%` Operator {1}".format(name, age)) # Printing Special Characters
print("Name: %s, Age: %d" % print("Name: {name}, Age: # Escape Sequences
(name, age)) {age}".format(name=name, print("He said, \"Hello,
age=age)) World!\"")
print("New line:\nSecond
line")
print("Tab:\tTabbed text")

Instructor : Musfiqul Hamid Showmik


STEM HERO BD

Input :

# Simple String Input # Boolean Input


name = input("Enter your name: ") is_student = input("Are you a student? (yes/no):
print(f"Hello, {name}!") ").lower() in ["yes", "y"]
# Integer Input print(f"Student status: {is_student}")
age = int(input("Enter your age: "))
print(f"Your age is {age}.") # List of Strings
colors = input("Enter your favorite colors,
# Float Input separated by commas: ").split(",")
height = float(input("Enter your height in colors = [color.strip() for color in colors]
meters: ")) print(f"Your favorite colors are: {colors}")
print(f"Your height is {height} meters.")
# List of Integers
numbers = input("Enter numbers separated by
spaces: ").split()
numbers = [int(num) for num in numbers]
print(f"Numbers list: {numbers}")

If else condition / Nested:


score = int(input("Enter your score: ")) stored_username = "user123"
stored_password = "pass123"
if score >= 90 and score <= 100:
grade = 'A' username = input("Enter your username: ")
elif score >= 80 and score < 90: password = input("Enter your password: ")
grade = 'B'
elif score >= 70 and score < 80: if username == stored_username:
grade = 'C' if password == stored_password:
elif score >= 60 and score < 70: print("Access granted")
grade = 'D' else:
elif score >= 0 and score < 60: print("Incorrect password")
grade = 'F' else:
else: print("Username not found")
grade = None

if grade:
print(f"Your grade is: {grade}")
else:
Instructor : Musfiqul Hamid Showmik
STEM HERO BD
print("Invalid score.")

Loop :
# Looping Through a Sequence # Looping with Range
print("Looping Through a Sequence:") print("\nLooping with Range:")
fruits = ["apple", "banana", "cherry"] # Range from 0 to 4
for fruit in fruits: for i in range(5):
print(fruit) print(i)
# Range from 1 to 5
# Looping With Index print("\nRange from 1 to 5:")
print("\nLooping With Index:") for i in range(1, 6):
for index, fruit in enumerate(fruits): print(i)
print(f"Index: {index}, Fruit: {fruit}")
# Range with a step of 2
print("\nRange with a step of 2:")
for i in range(0, 10, 2):
print(i)

Instructor : Musfiqul Hamid Showmik

You might also like