
| Intro To Python - Hello World Program |
| # This is a comment. It is not executed by Python. # It is used to explain the code to someone reading it. # The print function outputs text to the screen. print("Hello, World!") |
| Output- Hello, World! |
| # This is a single-line comment # Comments generally starts from hashtag. And have no impact on code. |
| if True: print("This is indented") |
| x = 5 y = "Hello" |
| a = 10 # Integer b = 20.5 # Float c = "Python" # String d = [1, 2, 3] # List e = (4, 5, 6) # Tuple f = {"name": "Alice", "age": 25} # Dictionary g = {1, 2, 3} # Set |
| print("Hello, World!") print(“My Name is Sahil”) |
| Output- Hello, World My Name is Sahil |
| sum = 5 + 3 # Arithmetic operator is_equal = (x == 5) # Comparison operator is_true = (x > 0 and y == "Hello") # Logical operator |
| if x > 0: print("x is positive") elif x == 0: print("x is zero") else print("x is negative") |
| import math print(math.sqrt(16)) |
| Sample Code Of Intro To Python |
| # Grocery Shopping List Program # Initialize an empty list to store the grocery items grocery_list = [ ] def show_menu(): print("\nMenu:") print("1. Add item") print("2. Remove item") print("3. View list") print("4. Quit") def add_item(): item = input("Enter the item to add: ") grocery_list.append(item) print(f"{item} has been added to the list.") def remove_item(): item = input("Enter the item to remove: ") if item in grocery_list: grocery_list.remove(item) print(f"{item} has been removed from the list.") else: print(f"{item} is not in the list.") def view_list(): if grocery_list: print("\nGrocery List:") for item in grocery_list: print(f"- {item}") else: print("The grocery list is empty.") while True: show_menu() choice = input("Choose an option: ") if choice == '1': add_item() elif choice == '2': remove_item() elif choice == '3': view_list() elif choice == '4': print("Goodbye!") break else: print("Invalid option. Please choose again.") |
| Output- Menu: 1. Add item 2. Remove item 3. View list 4. Quit Choose an option: 1 Enter the item to add: Milk Milk has been added to the list. Menu: 1. Add item 2. Remove item 3. View list 4. Quit Choose an option: 3 Grocery List: - Milk Menu: 1. Add item 2. Remove item 3. View list 4. Quit Choose an option: 2 Enter the item to remove: Milk Bread has been removed from the list. Menu: 1. Add item 2. Remove item 3. View list 4. Quit Choose an option: 3 Grocery List: - Empty Menu: 1. Add item 2. Remove item 3. View list 4. Quit Choose an option: 4 Goodbye! |