Introduction to Python - Basics #1
Python is a high-level, beginner-friendly programming language used in robotics, automation, game development, and artificial intelligence. It is known for its simple and readable syntax, making it a great first language for learning programming.
1. Printing Output in Python
One of the first things you'll do in Python is display text on the screen using the print() function.
print("Hello, World!")
Output:
Hello, World!
print()is used to display messages in Python.- Text must be inside quotation marks (
" "or' '). - Every statement in Python runs from top to bottom.
2. Taking User Input
Python allows users to enter data using the input() function:
name = input("Enter your name: ")
print("Hello, " + name + "!")
Example Output: If the user types Hannah, the output will be:
Hello, Hannah!
- The
input()function takes user input as a string. - We can use
+to concatenate (join) text together.
3. Variables and Data Types
In Python, a variable is used to store values, such as numbers or text:
age = 15
height = 5.7
name = "Hannah"
Common Data Types:
int→ Whole numbers (e.g.,10,25,42).float→ Decimal numbers (e.g.,3.14,7.89).str→ Text (e.g.,"Hello","Python").bool→ True/False values (e.g.,True,False).
4. Type Conversion
When using input(), Python always treats the input as a string. To use numbers, we need to convert the type:
age = input("Enter your age: ") # Input is stored as a string
age = int(age) # Convert to integer
print("Next year, you will be " + str(age + 1) + " years old!")
int()→ Converts a string to an integer.float()→ Converts a string to a decimal number.str()→ Converts numbers back to text.
Now that you've learned the basics, try answering the questions below! 👇
1. What will the following code output?
print("Hello, World!")
2. Which function allows a user to enter input in Python?
3. Which of the following correctly stores user input in a variable?
4. What will the following code output?
name = "Alex"
print(name)
5. What data type is stored in the variable: user_age?
6. What symbol is used for writing comments in Python?
7. Which of the following is a valid variable name in Python?
8. How do you convert user input to an integer in Python?
9. What happens if you try to add an integer and a string in Python?
10. What will happen if you forget parentheses in `print "Hello"`?