Visit Website

Python Basics for Beginners: Numbers, Strings & Lists

Python Basics for Beginners: Numbers, Strings & Lists

In this post, we’ll explore three fundamental building blocks of Python programming: Numbers, Strings, and Lists. Whether you're new to Python or need a refresher, this guide is packed with hands-on examples and explanations to help you build a solid foundation.

1. Numbers in Python

Python supports various numeric types. In this section, we’ll focus on two primary types:

  • Integers: Whole numbers, e.g., 1, -100, 5000

  • Floating-point numbers (floats): Numbers with decimal points or scientific notation, e.g., 2.5, -0.1, 3e2

Basic Arithmetic

Python can function as a powerful calculator. Here's a breakdown of operations:

Operation Python Code Result
Addition 2 + 3 5
Subtraction 5 - 2 3
Multiplication 3 * 4 12
Division 7 / 2 3.5
Floor Division 7 // 2 3
Modulo (remainder) 7 % 2 1
Power 2 ** 3 8
Root 4 ** 0.5 2.0

Note: Floor division truncates the decimal and returns an integer.

Variable Assignment

You can assign values to variables:

a = 10
b = a + 5
print(b)  # Output: 15

Reassign values using the variable itself:

a = a + a  # Now a is 20

Variable Naming Rules

  • Must start with a letter or underscore (_)

  • Cannot start with a number

  • No spaces or special characters

  • Follow lowercase and descriptive naming (e.g., tax_rate)

2. Strings in Python

Strings are sequences of characters used to store text data.

Creating Strings

Use single or double quotes:

"Hello"
'Python'
"Now I'm coding in Python!"

Printing Strings

Use the print() function:

print("Hello World")
print("Use \n for new lines")  # Line break

String Length

len("Hello World")  # Output: 11

Indexing & Slicing

Strings are zero-indexed:

s = "Hello World"
s[0]     # 'H'
s[-1]    # 'd'
s[1:5]   # 'ello'
s[::-1]  # Reverse: 'dlroW olleH'

Immutability

Strings cannot be modified. You can create new strings using concatenation or formatting.

Concatenation & Repetition

greet = "Hi"
greet = greet + " there"
print(greet)  # 'Hi there'

print("Hello! " * 3)  # 'Hello! Hello! Hello! '

Useful String Methods

s = "Python Is Fun"
s.upper()        # 'PYTHON IS FUN'
s.lower()        # 'python is fun'
s.split()        # ['Python', 'Is', 'Fun']
s.count("o")     # 1

You can list all methods using:

[i for i in dir(str) if not i.startswith("_")]

String Formatting

Using .format():

"Hello, my name is {}".format("Alice")

Using f-strings (Python 3.6+):

name = "Alice"
f"Hello, my name is {name}"

3. Lists in Python

Lists are ordered sequences that can hold any data type and are mutable (can be changed).

Creating Lists

my_list = [1, 2, 3]
mixed_list = ["Text", 123, 4.5]

Length, Indexing, and Slicing

len(my_list)     # 3
my_list[0]       # 1
my_list[1:]      # [2, 3]

Concatenation & Duplication

my_list + [4, 5]     # [1, 2, 3, 4, 5]
my_list * 2          # [1, 2, 3, 1, 2, 3]

Updating Lists

Lists can be modified:

my_list.append(4)     # Add to end
my_list.pop()         # Remove last item
my_list.remove(2)     # Remove specific item

Common List Methods

my_list.sort()        # Sort list
my_list.reverse()     # Reverse list

📎 View all list methods with:

[i for i in dir(list) if not i.startswith("_")]

Nesting Lists

Lists can contain other lists:

nested = [[1, 2], [3, 4]]
nested[0][1]  # 2

Summary

Here’s what you’ve learned so far:

✅ Basic operations with numbers
✅ Creating and formatting strings
✅ Using lists for collections of data
✅ Indexing, slicing, and using built-in methods
✅ Understanding Python's mutability and immutability

What’s Next?

In the next post, we’ll explore dictionaries, tuples, and sets—other core data structures in Python that offer unique capabilities.

Post a Comment

Visit Website
Visit Website
Mausam Welcome to WhatsApp chat
Hello! How can we help you today?
Type here...