#PYTHON(DAY48)
Example Programs
1) Python Program to Randomly Select an Element From the List
import random
my_list = [1, 'a', 32, 'c', 'd', 31]
print(random.choice(my_list))
And the output is:
31
2) Python Program to Print Output Without a Newline
# print each statement on a new line
print("Python")
print("is easy to learn.")
# new line
print()
# print both the statements on a single line
print("Python", end=" ")
print("is easy to learn.")
And the output is:
Python
is easy to learn.
Python is easy to learn.
3) Python Program to Get a Substring of a String
my_string = "I love python."
# prints "love"
print(my_string[2:6])
# prints "love python."
print(my_string[2:])
# prints "I love python"
print(my_string[:-1])
And the output is:
love
love python.
I love python
No comments:
Post a Comment