Python Programming Basics: Essential Code Snippets
Classified in Computers
Written on in English with a size of 4.07 KB
Python Fundamentals: Practical Code Examples
This document presents a collection of basic Python code snippets, demonstrating fundamental programming concepts such as conditional statements, loops, data input, and list manipulation. Each example is designed to illustrate a specific programming principle, making it ideal for beginners learning Python.
Compare Car Top Speeds with Python
This Python script compares two user-entered car speeds to determine which is faster, showcasing basic input and conditional logic.
car_a = int(input("Please enter the top speed of a Ferrari: "))
car_b = int(input("Please enter the top speed of a Lamborghini: "))
if car_a >= car_b:
print("The fastest car is a Ferrari.")
elif car_b > car_a:
print("The fastest car is a Lamborghini.")
Classify Pirate Treasure Size
This example takes the number of coins found by a pirate and classifies the treasure as small, medium, or big based on predefined ranges. It demonstrates if-elif-else statements for range checking.
coins = int(input("Please enter the number of coins the pirate found (max 1000), then press Enter: "))
if 1 <= coins <= 100:
print("This is a small treasure.")
elif 101 <= coins <= 500:
print("This is a medium treasure.")
elif 501 <= coins <= 1000:
print("This is a BIG treasure!")
Determine Bike Riding Eligibility by Age
A simple script to check if a person is old enough to ride a bike, based on their age. This illustrates a basic conditional check.
age = int(input("To know if you are old enough to ride a bike, please enter your age: "))
if age > 16:
print("You can ride a bike.")
else:
print("You can't ride a bike until you are 16.")
Combine Favorite Hobbies and Foods
This Python snippet demonstrates how to define and combine lists of favorite items, then print them. It highlights list creation and concatenation.
# The things that I like to do (my hobbies) are these:
games = ["Play basketball", "Play PlayStation", "Play Minecraft", "Play ping pong"]
print(games)
# My favorite foods are:
foods = ["Pizza", "Broccoli", "Avocado", "Chips", "Chorizo"]
# Combine hobbies and foods
favorites = games + foods
print("These are my favorite food and Hobbies:")
print(favorites)
Access List Elements by Index
This example shows how to create a list and access its elements using an index provided by the user. It's a fundamental concept for working with ordered data collections.
house_list = ("Blue", "Red", "Orange", "Purple") # Using a tuple here, which is immutable
position = int(input("Please put a number between 0 and 3: "))
print(house_list[position])
print("Please press Enter") # Original instruction, kept as per requirements
Looping Treasure Classification
This code extends the treasure classification example by placing it inside a for
loop, allowing the user to classify treasure multiple times. It demonstrates iteration and repeated conditional logic.
for count in range(5):
coins = int(input("Please enter the number of coins the pirate found (max 1000), then press Enter: "))
if 1 <= coins <= 100:
print("This is a small treasure.")
elif 101 <= coins <= 500:
print("This is a medium treasure.")
elif 501 <= coins <= 1000:
print("This is a BIG treasure!")
Calculate Player Lives in a Game
This script calculates a player's total lives based on different types of "hearts" and "stolen hearts," demonstrating arithmetic operations and string formatting for output.
red_hearts = int(input("Please input how many red hearts you have: "))
green_hearts = int(input("Please input how many green hearts you have: "))
stolen_red_hearts = int(input("Please input how many red hearts you have been stolen: "))
lives_left = red_hearts + (green_hearts * 100) - stolen_red_hearts
print("You have {0} lives left.".format(lives_left))