๐ง๐ซ Module 2: Python Basics (Core Concepts) – Kid-Friendly Guide
๐ What Are Variables and Data Types in Python?
A variable is like a container that holds information. Imagine having a box where you store things like numbers, words, or answers to questions. In Python, you give that box a name and store the data you want to use later.
Here’s how you create a variable in Python:
In this example:
nameis a variable that stores the text (or string) "Alex".ageis a variable that stores the number 12.is_hungryis a variable that stores a boolean value (True or False).
Python can handle different types of data. Here are the main ones you’ll encounter:
๐ Common Data Types in Python:
| Data Type | Description | Example |
|---|---|---|
int | Whole numbers | 5, 100, -20 |
float | Decimal numbers | 3.14, 0.5, -10.2 |
string | Text (enclosed in quotes) | "Hello", 'Python' |
boolean | True or False values | True, False |
Each type of data is used in different ways. For example, you would use integers to calculate someone's age, strings to display a name, and booleans to check whether a condition is true or false.
๐ฅ Taking Input and Displaying Output in Python
One of the coolest things about programming is making your code interactive. In Python, you can ask users for input and show them the output using two built-in functions:
input()– Used to take input from the user.print()– Used to display output on the screen.
Let’s look at a simple example:
๐ What’s Happening Here?
input()asks the user to type something (like their name).- The text they type is saved in the variable
name. print()displays a message on the screen that says, “Hello, [name]!”
You can also use input to get numbers from users:
In this example:
- The
input()function always returns a string (text). - We convert the string to an integer using
int()to perform math operations. - We use
str()to convert the number back to a string for display.
๐ What is Type Casting and Type Conversion?
Sometimes, the data you receive isn’t in the format you need. For example, if you ask a user to enter their age, it will be treated as text (a string). But if you want to perform calculations with that number, you’ll need to convert it to an integer.
This process is called type casting or type conversion.
Here’s how you can do it in Python:
| Function | Description | Example |
|---|---|---|
int() | Converts a string to an integer | int("5") → 5 |
float() | Converts a string to a decimal number | float("3.14") → 3.14 |
str() | Converts a number to a string | str(10) → "10" |
Let’s apply this in a program:
In this program:
- We take the user’s input as a string.
- We convert it to an integer to add 1 to their age.
- We convert it back to a string to display the result.
➕ Operators in Python: Doing Math and Making Comparisons
Just like in math, you can use operators in Python to perform calculations or compare values.
๐งฎ Arithmetic Operators:
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 → 8 |
- | Subtraction | 10 - 4 → 6 |
* | Multiplication | 2 * 3 → 6 |
/ | Division | 10 / 2 → 5.0 |
% | Modulus (remainder) | 7 % 3 → 1 |
Let’s try a quick program:
⚖️ Comparison Operators:
Comparison operators help us compare two values to see if they are equal, greater than, or less than each other.
| Operator | Description | Example |
|---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 10 > 3 → True |
< | Less than | 4 < 7 → True |
>= | Greater than or equal to | 8 >= 8 → True |
<= | Less than or equal to | 5 <= 9 → True |
Here’s an example program:
๐ค Working with Strings in Python
A string is simply a piece of text. Python makes it easy to work with strings using different techniques.
✂️ String Concatenation:
You can combine two strings using the + operator. This is called concatenation.
๐ Example:
๐ช String Slicing:
You can slice a string to get a part of it.
๐ Example:
๐ง String Methods:
Python has built-in methods that make it easy to work with strings.
| Method | Description | Example |
|---|---|---|
.upper() | Converts text to uppercase | "hello".upper() → HELLO |
.lower() | Converts text to lowercase | "HELLO".lower() → hello |
.replace() | Replaces part of a string | "Hello, World".replace("World", "Python") → Hello, Python |
.strip() | Removes spaces from both ends | " Hello ".strip() → Hello |
Here’s a quick program using string methods:
๐ฏ Summary
In this lesson, we learned:
- Variables are like containers that store information.
- Python has different data types like integers, floats, strings, and booleans.
- Input and output functions make your code interactive.
- Type casting allows you to convert data types.
- Operators let you do math and compare values.
- Strings are pieces of text that you can manipulate in various ways.
With these basics, you're well on your way to becoming a Python pro! ๐ Keep practicing, and don’t be afraid to try out your own ideas in code.
Comments
Post a Comment