The Basics of Python Programming: A Comprehensive Summary
Nidhi Krishna P V
Data Science | Full-Stack Development | MCA | AI & Machine Learning Enthusiast
Python is one of the easiest programming languages to learn, yet powerful enough to be used in web development, automation, data science, AI, and more. In this article, we’ll cover the essential building blocks of Python, helping you kickstart your programming journey.
1. Writing Your First Python Program
Python programs are simple to write. Open any Python environment (such as an online compiler or your terminal) and type:
print("Hello, World!")
When you run this, it will display:
Hello, World!
This is the traditional first program in any language. The print() function outputs text to the screen.
2. Variables and Data Types
Variables store data in Python. You don’t need to declare the type explicitly—Python automatically assigns it based on the value.
Common Data Types in Python
x = 10 # Integer
y = 3.14 # Float
name = "John" # String
is_python_fun = True # Boolean
numbers = [1, 2, 3, 4] # List
You can check the type of any variable using type():
print(type(x)) # Output: <class 'int'>
3. Operators in Python
Python supports various operators:
Example:
a = 5
b = 2
print(a + b) # 7
print(a ** b) # 5^2 = 25
print(a > b) # True
4. Conditional Statements (if-elif-else)
Python allows decision-making with if, elif, and else.
age = 18
if age >= 18:
print("You are eligible to vote.")
elif age == 17:
print("Wait for a year.")
else:
print("You are too young to vote.")
5. Loops (for and while)
Loops allow repeating a block of code multiple times.
For Loop
Used to iterate over a sequence like lists or ranges.
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
While Loop
Runs as long as a condition is true.
count = 1
while count <= 5:
print(count)
count += 1
领英推荐
6. Functions in Python
Functions help organize code into reusable blocks.
def greet(name):
return f"Hello, {name}!"
print(greet("Alice"))
Output:
Hello, Alice!
7. Lists and Tuples
Lists (Mutable)
A list is an ordered collection of items that can be changed.
fruits = ["Apple", "Banana", "Cherry"]
fruits.append("Mango")
print(fruits)
Output:
['Apple', 'Banana', 'Cherry', 'Mango']
Tuples (Immutable)
Similar to lists, but they cannot be changed after creation.
colors = ("Red", "Green", "Blue")
print(colors[0]) # Output: Red
8. Dictionaries (Key-Value Pairs)
Dictionaries store data as key-value pairs.
student = {"name": "John", "age": 21, "course": "Python"}
print(student["name"]) # Output: John
You can also add new key-value pairs:
student["grade"] = "A"
9. Exception Handling (try-except)
Python handles errors using try-except blocks.
try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")
Output:
Cannot divide by zero!
10. File Handling in Python
Python allows reading and writing files easily.
Writing to a File
with open("test.txt", "w") as file:
file.write("Hello, Python!")
Reading a File
with open("test.txt", "r") as file:
content = file.read()
print(content)
Conclusion
This is just the tip of the iceberg! Python is vast, and mastering it requires practice. Start experimenting with these basics, and soon, you’ll be building your own projects.
Want to dive deeper? Stay tuned for more articles in Code Chronicles By Nidhi!
Incredible post!?Nidhi Krishna P V
Great basics et introduction, it's useful for beginner want learn python If people want start with python course with this article, i suggest to start with a python compilor or even to download anaconda and to use jupyter to use these codes