Here are practical Python code examples demonstrating core concepts like arithmetic, lists, functions, and dictionaries, complete with expected outputs.
Basic Arithmetic and Variables You can perform calculations and assign variables without explicitly defining their types. The following example adds, subtracts, multiplies, and divides two numbers:
a = 10
b = 5
print("Addition:", a + b)
print("Subtraction:", a – b)
print("Multiplication:", a * b)
print("Division:", a / b)Output: Addition: 15, Subtraction: 5, Multiplication: 50, Division: 2.0.
List Slicing and Operations Python lists support slicing to extract subsets of data and methods to modify them. You can reverse a list or select elements with a specific step:
my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
slice1 = my_list[2:6]
slice5 = my_list[::-1]
print(slice1) # Output: [2, 3, 4, 5]
print(slice5) # Output: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry', 'orange']Functions and Dictionaries Functions allow you to define reusable logic, while dictionaries store key-value pairs. You can merge dictionaries or retrieve specific values:
def greet(name):
return f"Hello, {name}!"
print(greet("Ankit")) # Output: Hello, Ankit!
student = {"name": "John", "age": 22, "major": "Physics"}
print(student["name"])
print(student.get("major"))
dict1 = {"name": "Alice", "age": 25}
dict2 = {"city": "New York", "job": "Engineer"}
dict1.update(dict2)
print(dict1) # Output: {'name': 'Alice', 'age': 25, 'city': 'New York', 'job': 'Engineer'}Advanced Concepts For more complex tasks, Python supports f-strings for formatting, generators for memory efficiency, and loops for iteration.
F-strings:
print(f"Hi, I'm {first_name} and I'm {age} years old!")is faster and cleaner than.format().Generators: Using parentheses
(x * 2 for x in range(10))creates a generator that yields values on demand, saving memory compared to lists.Loops: To print numbers 1 to 100, use
for i in range(1, 101): print(i).
These examples cover fundamental syntax and logic used in Python programming, helping beginners transition from theory to practice.
I wrote my first Python code; example for newbie....
Python code examples?
Beginner Python projects?
automate the boring stuff gave me a lot of inspiration for smaller projects. it's full of examples and introduces you to incredibly useful concepts. for me, it filled in a lot of knowledge gaps that i had after doing various other tutorials. it's python 3 though (i believe codecademy is python 2?)
also, the community at r/roguelikedev is currently doing a tutorial that shows you how to make a simple roguelike game in python.
More on reddit.comPython Education
What Are Python Examples?
How do you code in Python?
Where can I find Python examples?
Videos
So taking up suggestions from my "gibberish" post yesterday (thanks to all who replied, not sure why mods removed it), I decided to continue my days-old project of learning Python by actually sitting down to write my first code. Why not? Let's give it a try.
After learning about the # symbol and how it provides "commentary" for the lines of code, I decided to write down my proposed code within the # lines. I asked myself, what do I want the result to be? Here, I decided a friend says either they like my red shirt or doesn't say anything about the shirt if it is a color other than red. And then I asked myself, what are the steps to get there? Here, I said there has to be an action for what the friend will say (the print command determined by a "red or else" function) and also a variable that is fill-in-the-blank for either red or not red (the variable).
This took me several tries and I did encounter a few errors. These were syntax errors relating to the correct variable and function terminology. But otherwise about 10 minutes from beginning to successful end. I'm pretty proud of this. Hopefully this post helps another Python newbie out there.
# This code will have a friend say either one of two things in response to seeing me, depending upon the color of my shirt.
#
# If my shirt is red, my friend will say hello and say he likes my shirt.
#
# But if my shirt is not red, my friend will just say hello.
#
# My code needs these items: A fill-in-the-blank for the color of my shirt, red or otherwise.
# My code also needs some kind of function which determines what my friend will say depending upon the color of my shirt.
my_shirt_color_today = ["red"]
if my_shirt_color_today == ["red"]:
print("Hello friend, I like the color of your shirt! Red is my favorite")
else:
print("Hello friend! Why didn't you wear your red shirt today?")Hi, I'm new here. Learning python.
I've been taking some courses online. Their explanation seems to be a bit difficult to understand. I think it could be better if the codes were simply written in simple terms with examples and how the code works were explained in the comments (like the below e.g.). It would've been better to understand and learn quickly this way. Is there a resource available where I can find this kind of explanation?
Your help will be much appreciated.
x = "abcdefghijk" print(x[::2]) #Outputs acegik