50 Practical and Simple Python Codes for Beginners
Want to learn Python in a simple and practical way? Here are 50 easy-to-understand Python examples with short explanations. These will help you grasp Python basics quickly and effectively.
Section 1: Basics (1 – 10)
-
Print text on the screen
Prints the text between the quotes on the screen.
-
Add two numbers
Prints the sum of the numbers (8).
-
User input
Takes input from the user and prints a greeting.
-
Check variable type
Prints the variable type (int).
-
Convert text to number
Converts the input to a number and multiplies it by 2.
-
If condition
Checks if the score is passing or failing.
-
For loop
Prints numbers from 0 to 4.
-
While loop
Repeats the process until count reaches 3.
-
Simple function
Defines and calls a simple function.
-
Using math library
Prints the square root of 16.
Section 2: Working with Strings (11 – 20)
-
String length
Prints the number of characters in the string.
-
Convert to uppercase
Prints HELLO.
-
Convert to lowercase
Prints welcome.
-
Replace text
Replaces "Java" with "Python".
-
Check if word exists
Returns True if the word exists.
-
Split a string into words
Splits the string into a list of words.
-
Join list into a string
Joins list items into a string (Py-thon).
-
Reverse a string
Prints cba.
-
Trim spaces
Removes extra spaces at the start and end.
-
Count occurrences
Prints how many times "a" appears (3).
Section 3: Lists (21 – 30)
-
Create a list
Creates a list of fruits.
-
Add an item
Adds "orange" to the list.
-
Remove an item
Removes "banana" from the list.
-
Access a specific item
Prints the first item.
-
Access the last item
Prints the last item.
-
List length
Prints the number of items in the list.
-
Sort a list
Sorts numbers in ascending order.
-
Reverse a list
Reverses the list order.
-
Check if item exists
Returns True if "apple" exists in the list.
-
Merge two lists
Combines both lists.
Section 4: Dictionaries (31 – 40)
-
Create a dictionary
Creates a dictionary with key-value pairs.
-
Access a value
Prints the value of "name".
-
Update a value
Updates the "age" key.
-
Add a new key-value pair
Adds a new key-value pair.
-
Delete a key
Deletes the "city" key.
-
Loop through a dictionary
Prints all keys and values.
-
Get all keys
Prints all keys.
-
Get all values
Prints all values.
-
Check if key exists
Returns True if the key exists.
-
Clear the dictionary
Removes all items from the dictionary.
Section 5: Fun & Practical (41 – 50)
-
Random choice
Chooses a random item from the list.
-
Random number
Generates a random number between 1 and 10.
-
Current date and time
Prints the current date and time.
-
Measure execution time
Measures how long the code takes to run.
-
Read a file
Reads and prints a file’s content.
-
Write to a file
Writes text to a file.
-
Handle errors
Handles errors without stopping the program.
-
Draw a circle with turtle
Draws a simple circle.
-
Convert to JSON
Converts a dictionary to JSON format.
-
Simple calculator
Performs basic arithmetic operations (+, -, *, /).
With these 50 simple Python codes, you’ve covered the essential basics to start coding confidently. Practice them, modify them, and try creating your own projects!

0 Comments