Created
April 17, 2023 11:18
-
-
Save nfl0/41a245f2b82740985e1e58bf9c16870a to your computer and use it in GitHub Desktop.
This Python script demonstrates various programming techniques including conditional statements, loops, lists, dictionaries, functions, classes, file handling, random numbers, and system commands. Each programming technique is accompanied by examples and comments for clarity and understanding. Use this script as a reference or tutorial for learn…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Importing modules | |
import os | |
import random | |
# Declaring variables | |
num1 = 5 | |
num2 = 10 | |
name = "John" | |
# Conditional statements | |
if num1 < num2: | |
print("num1 is smaller than num2") | |
else: | |
print("num1 is greater than or equal to num2") | |
# Loops | |
for i in range(1, 11): | |
# Check if i is even | |
if i % 2 == 0: | |
print(i, "is an even number") | |
else: | |
print(i, "is an odd number") | |
# Lists | |
fruits = ["apple", "banana", "cherry"] | |
# Add an element to the list | |
fruits.append("orange") | |
# Print the list | |
print("The list of fruits is:", fruits) | |
# Sort the list in reverse order | |
fruits.sort(reverse=True) | |
# Print the sorted list | |
print("The sorted list of fruits is:", fruits) | |
# Remove the last element from the list | |
fruits.pop() | |
# Print the updated list | |
print("The updated list of fruits is:", fruits) | |
# Dictionaries | |
person = {"name": "John", "age": 36, "country": "USA"} | |
# Print the value of the age key | |
print("The person's age is:", person["age"]) | |
# Update the value of the age key | |
person["age"] = 37 | |
# Print the updated value of the age key | |
print("The person's age is now:", person["age"]) | |
# Add a new key-value pair to the dictionary | |
person["city"] = "New York" | |
# Print the updated dictionary | |
print("The updated person dictionary is:", person) | |
# Functions | |
def greet(name): | |
print("Hello, " + name) | |
# Get user input for the name | |
user_name = input("Please enter your name: ") | |
# Call the greet function with the user's name | |
greet(user_name) | |
# Classes | |
class Car: | |
def __init__(self, make, model): | |
self.make = make | |
self.model = model | |
def start(self): | |
print("The " + self.make + " " + self.model + " is starting...") | |
# Create an instance of a car and start it | |
my_car = Car("Toyota", "Corolla") | |
my_car.start() | |
# File handling | |
# Open a file in write mode | |
file = open("example.txt", "w") | |
# Write a string to the file | |
file.write("This is an example of file handling in Python.") | |
# Close the file | |
file.close() | |
# Random numbers | |
# Generate a random integer between 1 and 100 | |
random_number = random.randint(1, 100) | |
# Print the random number | |
print("Here is a random number between 1 and 100:", random_number) | |
# System commands | |
# Get the current working directory | |
current_directory = os.getcwd() | |
# Print the current working directory | |
print("The current working directory is:", current_directory) | |
# List the contents of the current directory | |
os.system("ls") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment