Python code examples | Python Programming Example | Programming Code
Oct 27, 2023
Here are some simple Python code examples:
Python
# Print "Hello, world!"
print("Hello, world!")
Use code with caution. Learn more
Output:
Hello, world!
Python
# Add two numbers
num1 = 10
num2 = 20
sum = num1 + num2
print(sum)
Use code with caution. Learn more
Output:
30
Python
# Find the largest number in a list
list1 = [10, 20, 30, 40, 50]
largest_number = max(list1)
print(largest_number)
Use code with caution. Learn more
Output:
50
Python
# Reverse a string
string = "Hello, world!"
reversed_string = string[::-1]
print(reversed_string)
Use code with caution. Learn more
Output:
!dlrow ,olleH
Python
# Check if a number is prime
number = 11
def is_prime(number):
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
if is_prime(number):
print("The number is prime.")
else:
print("The number is not prime.")
Use code with caution. Learn more
Output:
The number is prime.
These are just a few simple Python code examples. There are many other things you can do with Python, such as developing web applications, machine learning models, and data science scripts. With a little practice, you can learn to use Python to solve all sorts of problems!
Show More Show Less 