Docsity

Python Programming: A Beginner's Guide, Study notes of Computer science

Partial preview of the text

Download Python Programming: A Beginner's Guide and more Study notes Computer science in PDF only on Docsity!

Python Basics & Popular Libraries

A concise guide (10 pages)

Covers: Syntax Data Types Control Flow Functions Classes Files Libraries

What is Python? Python is a popular high-level programming language known for its simple synt versatility. It is used in web development, data science, automation, artificial intellige and more.

2. Basics & Syntax

Variables and types

name = "Alice" # str age = 25 # int height = 5.7 # float is_student = True # bool

Printing

print(f"My name is {name}, I am {age} years old")

Comments

This is a comment

3. Data Types Numbers: int, float, complex String: "Hello" (immutable) List: [1, 2, 3] (mutable, ordered) Tuple: (1, 2, 3) (immutable, ordered) Dict: {"a": 1, "b": 2} (key-value) Set: {1, 2, 3} (unique, unordered) None: absence of value

5. Functions & Modules def greet(name: str) -> str: return f"Hello, {name}" print(greet("Alice"))

Importing modules

import math print(math.sqrt(16))

From-import

from random import randint print(randint(1, 10))

6. Classes & OOP class Dog: def init(self, name): self.name = name def bark(self): return f"{self.name} says woof!" d = Dog("Buddy") print(d.bark())

8. Popular Libraries NumPy numerical computing import numpy as np arr = np.array([1, 2, 3]) print(arr.mean()) Pandas data analysis import pandas as pd df = pd.DataFrame({"a":[1,2], "b":[3,4]}) print(df.head()) Matplotlib visualization import matplotlib.pyplot as plt plt.plot([1,2,3], [4,5,6]) plt.show() Requests web requests import requests r = requests.get("https://example.com") print(r.status_code)

9. Tips & Resources Use f-strings for formatting List comprehensions for cleaner loops Use virtual environments for projects Explore official docs: docs.python.org Practice on LeetCode, HackerRank, or Project Euler Great learning sites: Real Python, W3Schools, GeeksforGeeks