Here are 10 different cool things you can make a computer do in just 10 lines of Python code:
- Create a simple calculator that can perform addition, subtraction, multiplication, and division:
a = int(input("Enter first number: ")) b = int(input("Enter second number: ")) op = input("Enter operator (+, -, *, /): ") if op == '+': print(a + b) elif op == '-': print(a - b) elif op == '*': print(a * b) elif op == '/': print(a / b)
2. Generate a random password of a given length with a combination of letters, numbers, and symbols:
import random import string length = 12 characters = string.ascii_letters + string.digits + string.punctuation password = ''.join(random.choice(characters) for i in range(length)) print(password)
3. Display the Fibonacci sequence up to a certain number of terms:
n = int(input("Enter number of terms: ")) a, b = 0, 1 for i in range(n): print(a) a, b = b, a + b
- Play a guessing game where the computer picks a random number and the user has to guess it:
import random secret_number = random.randint(1, 100) guess = 0 while guess != secret_number: guess = int(input("Guess the number (between 1 and 100): ")) if guess < secret_number: print("Too low!") elif guess > secret_number: print("Too high!") print("Congratulations, you guessed the number!")
- Draw a colorful spiral using the Turtle graphics library:
import turtle t = turtle.Turtle() colors = ["red", "orange", "yellow", "green", "blue", "purple"] for i in range(360): t.pencolor(colors[i % 6]) t.width(i / 100 + 1) t.forward(i) t.left(59) turtle.done()
6. Implement a basic chatbot that can respond to user input:
name = input("Hi, what's your name? ") print("Hello " + name + "!") age = int(input("How old are you? ")) if age >= 18: print("You're an adult!") else: print("You're a minor!")
7. Download an image from a given URL:
import requests url = "https://www.example.com/image.jpg" response = requests.get(url) with open("image.jpg", "wb") as f: f.write(response.content)
8. Convert a number from decimal to binary:
n = int(input("Enter a number: ")) binary = bin(n) print(binary)
9. Use regular expressions to extract phone numbers from a text:
import re text = "Call me at 123-456-7890 or 987-654-3210" phone_numbers = re.findall(r'\d{3}-\d{3}-\d{4}', text) print(phone_numbers)
10. Generate a word cloud from a given text using the wordcloud library:
from wordcloud import WordCloud import matplotlib.pyplot as plt text = "Hello world, this is a sample text for a word cloud." wordcloud = WordCloud().generate(text) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show()
These are just a few examples of the many things you can do with just 10 lines of Python code!