Python is one of the easiest and most popular programming languages, used in web development, data science, artificial intelligence and automation. If you already know some C++, this beginner guide shows you Python syntax side by side, with examples you can run straight away.
Getting Started
- Download Python 3 from the official site python.org (on Windows, tick "Add Python to PATH").
- Write code in IDLE, VS Code or an online editor such as Google Colab.
- Run a file with
python hello.py.
print("Hello, GCUF!")Variables and Data Types
Python has no type declarations; the type is decided by the value.
name = "Ali" # str
age = 19 # int
cgpa = 3.45 # float
is_student = True # bool
print(type(cgpa)) # <class 'float'>Input and Output
marks = int(input("Enter marks out of 100: "))
print("You got", marks)
print(f"That is {marks}% of the total")input() always returns text, so convert it with int() or float().
Indentation Instead of Braces
Python uses indentation (4 spaces) to mark blocks instead of { }.
if marks >= 85:
print("Grade A")
elif marks >= 50:
print("Pass")
else:
print("Fail")Loops
for i in range(1, 11): # 1 to 10
print(5, "x", i, "=", 5 * i)
count = 3
while count > 0:
print(count)
count -= 1Lists and Dictionaries
marks = [78, 65, 90, 55]
marks.append(84)
print(sum(marks) / len(marks)) # average
student = {"name": "Sara", "cgpa": 3.8}
print(student["name"])Functions
def square(n):
return n * n
print(square(6)) # 36Python vs C++ at a Glance
| Feature | Python | C++ |
|---|---|---|
| Typing | Dynamic | Static |
| Blocks | Indentation | Braces { } |
| Semicolons | Not needed | Required |
| Speed | Slower (interpreted) | Faster (compiled) |
| Best for | Data science, AI, scripting, web | Systems, games, performance-critical code |
Practice Tasks
- Ask for 5 marks and print the average and highest.
- Print all even numbers from 1 to 50.
- Write a function that returns the factorial of n.
- Store 3 students in a dictionary and print the one with the highest CGPA.
FAQ
Should I learn Python or C++ first?
Follow your course: most BSCS programmes start with C++. Learning Python alongside is easy and very useful for projects, data analysis and AI.
Is Python free?
Yes, Python is free and open-source.
Compare with C++ Basics for Beginners and find free courses on our resources page.
0 comments:
Post a Comment