Please open Telegram to view this post
VIEW IN TELEGRAM
100 Python Examples: A Step-by-Step Guide
#Python #Programming #Tutorial #LearnPython
Part 1: The Basics (Examples 1-15)
#1. Print "Hello, World!"
The classic first program.
#2. Variables and Strings
Store text in a variable and print it.
#3. Integer Variable
Store a whole number.
#4. Float Variable
Store a number with a decimal point.
#5. Boolean Variable
Store a value that is either
#6. Get User Input
Use the
#7. Simple Calculation
Perform a basic arithmetic operation.
#8. Comments
Use
#9. Type Conversion (String to Integer)
Convert a user's input (which is a string) to an integer to perform math.
#10. String Concatenation
Combine multiple strings using the
#11. Multiple Assignment
Assign values to multiple variables in one line.
#12. The
Check the data type of a variable.
#13. Basic Arithmetic Operators
Demonstrates addition, subtraction, multiplication, and division.
#14. Floor Division and Modulus
#15. Exponentiation
Use
---
Part 2: String Manipulation (Examples 16-25)
#16. String Length
Use
#Python #Programming #Tutorial #LearnPython
Part 1: The Basics (Examples 1-15)
#1. Print "Hello, World!"
The classic first program.
print() is a function that outputs text to the console.print("Hello, World!")Hello, World!
#2. Variables and Strings
Store text in a variable and print it.
message = "I am learning Python."
print(message)
I am learning Python.
#3. Integer Variable
Store a whole number.
age = 30
print("My age is:", age)
My age is: 30
#4. Float Variable
Store a number with a decimal point.
price = 19.99
print("The price is:", price)
The price is: 19.99
#5. Boolean Variable
Store a value that is either
True or False.is_learning = True
print("Am I learning?", is_learning)
Am I learning? True
#6. Get User Input
Use the
input() function to get information from the user.name = input("What is your name? ")
print("Hello, " + name)What is your name? Alice
Hello, Alice
#7. Simple Calculation
Perform a basic arithmetic operation.
a = 10
b = 5
print(a + b)
15
#8. Comments
Use
# to add comments that Python will ignore.# This line calculates the area of a rectangle
length = 10
width = 5
area = length * width
print("Area is:", area)
Area is: 50
#9. Type Conversion (String to Integer)
Convert a user's input (which is a string) to an integer to perform math.
age_str = input("Enter your age: ")
age_int = int(age_str)
next_year_age = age_int + 1
print("Next year you will be:", next_year_age)Enter your age: 25
Next year you will be: 26
#10. String Concatenation
Combine multiple strings using the
+ operator.first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
print(full_name)
John Doe
#11. Multiple Assignment
Assign values to multiple variables in one line.
x, y, z = 10, 20, 30
print(x, y, z)
10 20 30
#12. The
type() FunctionCheck the data type of a variable.
num = 123
text = "hello"
pi = 3.14
print(type(num))
print(type(text))
print(type(pi))
<class 'int'>
<class 'str'>
<class 'float'>
#13. Basic Arithmetic Operators
Demonstrates addition, subtraction, multiplication, and division.
a = 15
b = 4
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
#14. Floor Division and Modulus
// for division that rounds down, and % for the remainder.a = 15
b = 4
print("Floor Division:", a // b)
print("Modulus (Remainder):", a % b)
Floor Division: 3
Modulus (Remainder): 3
#15. Exponentiation
Use
** to raise a number to a power.power = 3 ** 4 # 3 to the power of 4
print(power)
81
---
Part 2: String Manipulation (Examples 16-25)
#16. String Length
Use
len() to get the number of characters in a string.my_string = "Python is fun"
print(len(my_string))
13
❤1