Python basics for beginners

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

  1. Download Python 3 from the official site python.org (on Windows, tick "Add Python to PATH").
  2. Write code in IDLE, VS Code or an online editor such as Google Colab.
  3. 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 -= 1

Lists 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))   # 36

Python vs C++ at a Glance

FeaturePythonC++
TypingDynamicStatic
BlocksIndentationBraces { }
SemicolonsNot neededRequired
SpeedSlower (interpreted)Faster (compiled)
Best forData science, AI, scripting, webSystems, games, performance-critical code

Practice Tasks

  1. Ask for 5 marks and print the average and highest.
  2. Print all even numbers from 1 to 50.
  3. Write a function that returns the factorial of n.
  4. 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.


Next
This is the most recent post.
Previous
Older Post

0 comments:

Post a Comment

 
Top