๐Ÿง‘‍๐Ÿซ 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:

python

name = "Alex" age = 12 is_hungry = True

In this example:

  • name is a variable that stores the text (or string) "Alex".
  • age is a variable that stores the number 12.
  • is_hungry is 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 TypeDescriptionExample
intWhole numbers5, 100, -20
floatDecimal numbers3.14, 0.5, -10.2
stringText (enclosed in quotes)"Hello", 'Python'
booleanTrue or False valuesTrue, 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:

  1. input() – Used to take input from the user.
  2. print() – Used to display output on the screen.

Let’s look at a simple example:

python

name = input("What's your name? ") print("Hello, " + name + "!")

๐Ÿ“ 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:

python

age = input("What's your age? ") print("Next year, you will be " + str(int(age) + 1) + " years old!")

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:

FunctionDescriptionExample
int()Converts a string to an integerint("5")5
float()Converts a string to a decimal numberfloat("3.14")3.14
str()Converts a number to a stringstr(10)"10"

Let’s apply this in a program:

python

age = input("What's your age? ") age = int(age) # Convert string to int print("Next year, you will be " + str(age + 1) + " years old!")

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:

OperatorDescriptionExample
+Addition5 + 38
-Subtraction10 - 46
*Multiplication2 * 36
/Division10 / 25.0
%Modulus (remainder)7 % 31

Let’s try a quick program:

python

a = 10 b = 5 print(a + b) # Addition print(a - b) # Subtraction print(a * b) # Multiplication print(a / b) # Division print(a % b) # Modulus (remainder)

⚖️ Comparison Operators:

Comparison operators help us compare two values to see if they are equal, greater than, or less than each other.

OperatorDescriptionExample
==Equal to5 == 5True
!=Not equal to5 != 3True
>Greater than10 > 3True
<Less than4 < 7True
>=Greater than or equal to8 >= 8True
<=Less than or equal to5 <= 9True

Here’s an example program:

python

x = 10 y = 7 print(x > y) # True print(x == y) # False print(x != y) # True

๐Ÿ”ค 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:

python

first_name = "Alex" last_name = "Smith" print(first_name + " " + last_name) # Alex Smith

๐Ÿ”ช String Slicing:

You can slice a string to get a part of it.

๐Ÿ“Œ Example:

python

word = "Python" print(word[0:3]) # Pyt print(word[-1]) # n

๐Ÿ”ง String Methods:

Python has built-in methods that make it easy to work with strings.

MethodDescriptionExample
.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:

python

text = " Hello, Python! " print(text.upper()) # HELLO, PYTHON! print(text.lower()) # hello, python! print(text.strip()) # "Hello, Python!" print(text.replace("Python", "World")) # Hello, World!

๐ŸŽฏ Summary

In this lesson, we learned:

  1. Variables are like containers that store information.
  2. Python has different data types like integers, floats, strings, and booleans.
  3. Input and output functions make your code interactive.
  4. Type casting allows you to convert data types.
  5. Operators let you do math and compare values.
  6. 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