Python is a simple and powerful programming language, widely used for web development, data analysis, artificial intelligence, and more. In this article, we will explore three basic Python commands essential for any beginner: print(), input(), and len().

1. The print() Command

The print() command is used to display output in the console. It allows printing messages, variable values, and expression results.

Example:

print("Hello, world!")

Output:

Hello, world!

We can also display multiple values separated by a comma:

name = "John"
age = 25
print("My name is", name, "and I am", age, "years old.")

Output:

My name is John and I am 25 years old.

2. The input() Command

The input() command is used to receive user input. Whatever is typed will be treated as a string.

Example:

name = input("Enter your name: ")
print("Hello,", name, "! Welcome!")

User Input:

Enter your name: Maria

Output:

Hello, Maria! Welcome!

If we want to receive a number, we need to convert the input:

age = int(input("Enter your age: "))
print("In 5 years, you will be", age + 5, "years old.")

3. The len() Command

The len() command is used to get the length of a data structure, such as a string, list, or tuple.

Example with strings:

message = "Python"
length = len(message)
print("The length of the word 'Python' is:", length)

Output:

The length of the word 'Python' is: 6

Example with lists:

numbers = [10, 20, 30, 40, 50]
print("The length of the list is:", len(numbers))

Output:

The length of the list is: 5

Conclusion

The commands print(), input(), and len() are fundamental for learning Python. The print() command allows displaying messages, the input() command receives user input, and the len() command helps measure the size of data structures. Mastering these commands is essential for writing interactive and dynamic programs.

Want to learn more? Keep following our tutorials at www.usandopy.com! 🚀

Copyright Notice: Unless otherwise indicated, all articles are original to this site, and reproduction must cite the source

Article link:http://pybeginners.com/article/basic-python-commands/