"10 Beginner Programs for Kids – Step-by-Step Guide"
------------------------------------------------------------------------------------------------------------------------------------------------------
1.
Write a Python program to print your name.
2.
Write a Python program to combine two strings.
3.
Write a Python program to check the length of a
string.
4.
Write a Python program to print the smallest and
largest numbers (using min() and max()).
5.
Write a Python program to repeat a string.
6.
Write a Python program to sort a list of numbers.
7.
Write a Python program to use the divmod()
function.
8.
Write a Python program to use the multiplication
operator.
9.
Write a Python program to add two numbers and print
the result.
10.
Write a Python program that uses the sum()
function.
1. Program
to Print Your Name
print("My name is John Doe")
- What is print()?
- print() is a function that
outputs whatever you tell it to, onto the screen.
- Functions are like instructions that tell to do something. In this case, it tells to display text.
- What is a string?
- A string is any sequence of characters
(letters, numbers, or symbols) inside quotes ("").
- "My name is John Doe" is
a string, because it's enclosed in quotes.
- Output:
When the code is run, will print:
2. Program
to Combine Two Strings
string1 =
"Hello"
string2 =
"World"
combined_string
= string1 + " " + string2
print(combined_string)
- What is a variable?
- A variable is like a box where you
store data. Here, string1 holds the text "Hello",
and string2 holds "World".
- What does + do
with strings?
- The +
operator joins (concatenates) two strings. In this case, it
combines string1 and string2
with a space " " in
between.
- Why is there a space?
- Strings will stick together if you don’t add
a space. Adding " "
ensures there’s a space between "Hello"
and "World".
- Output:
When you run the program, it prints:
my_string
= " "
length_of_string
= len(my_string)
print("Length
of the string is:", length_of_string)
- What does len() do?
- len() is a function that
counts the number of characters in a string, including spaces and
punctuation.
- Example:
In " ", there are 6 characters: P, y, t, h, o, n. So, len(my_string) will return 6. - Output:
The program prints:
string = "Hello
"
repeated_string
= string * 3
print(repeated_string)
- What does * do
with strings?
- The *
operator repeats a string a given number of times. Here, string
* 3 repeats "Hello "
three times.
- Example:
If you repeat "Hello " 3 times, you get "Hello Hello Hello ". - Output:
The result is:
-
Hello
Hello Hello
----------------------------------------------------------------
5. Program
to Find the Smallest and Largest Number
numbers =
[10, 20, 5, 40, 15]
smallest_number
= min(numbers)
largest_number
= max(numbers)
print("Smallest
number is:", smallest_number)
print("Largest
number is:", largest_number)
- What is a list?
- A list is a collection of items
(numbers, strings, etc.) stored inside square brackets [].
Here, numbers = [10, 20, 5, 40, 15] is
a list of numbers.
- What does min() do?
- min() finds the smallest number
in the list.
- What does max() do?
- max() finds the largest number
in the list.
- Example:
In [10, 20, 5, 40, 15], the smallest number is 5, and the largest is 40. - Output:
The program prints:
numbers =
[30, 10, 50, 20, 40]
sorted_numbers
= sorted(numbers)
print("Sorted
numbers:", sorted_numbers)
- What does sorted() do?
- sorted() arranges (or sorts) the
numbers in the list from smallest to largest.
- Example:
The list [30, 10, 50, 20, 40] becomes [10, 20, 30, 40, 50] after sorting. - Output:
The program prints:
numerator
= 25
denominator
= 4
result = divmod(numerator,
denominator)
print("Quotient
and Remainder are:", result)
- What does divmod() do?
- divmod() returns two results:
the quotient (how many times one number fits into another) and the
remainder (what's left over).
- Example:
- For 25 ÷ 4,
the quotient is 6 because 4
fits into 25 six times, and the
remainder is 1 because there's 1
left after dividing.
- Output:
The program prints:
num1 = 7
num2 = 8
result =
num1 * num2
print("Multiplication
result is:", result)
- What does * do
with numbers?
- The *
operator multiplies two numbers.
- Example:
- Here, 7 * 8
equals 56.
- Output:
The result is:
num1 = 15
num2 = 25
sum_result
= num1 + num2
print("Addition
result is:", sum_result)
- What does + do
with numbers?
- The +
operator adds two numbers together.
- Example:
- Here, 15 + 25
equals 40.
- Output:
The result is:
10. Python
Program to Use the sum() Function
numbers =
[10, 20, 30, 40, 50]
total_sum
= sum(numbers)
print("The
sum of the numbers is:", total_sum)
Step-by-Step Explanation:
1. List of numbers:
o numbers = [10, 20, 30, 40, 50] is a list containing five
numbers.
2. What does sum() do?:
o sum() adds all the numbers in the list.
o In this example, sum(numbers) will compute 10 + 20 + 30 + 40 + 50, which equals 150.
3. Store the result:
o The total sum is stored in the
variable total_sum.
4. Print the result:
o print() displays the total sum on the screen.
Output:
When the
program is run, it will print:
The sum of the numbers is: 150
No comments:
Post a Comment