[大学院-python-base gammer learning2: python base programming ]

python-base gammer learning2: python base programming

  • 1:introduction
  • [2:computer setting development environment & Jupyter Notebook](#2:computer setting development environment & Jupyter Notebook)
    • [(1):computer setting development environment](#(1):computer setting development environment)
    • [(2):Jupyter Notebook](#(2):Jupyter Notebook)
  • [is used for comments](#is used for comments)
  • [3: python (1)-base](#3: python (1)-base)
  • [4: python (2)-loop](#4: python (2)-loop)
    • [(1):control structures](#(1):control structures)
    • [(2): Loops : for and while](#(2): Loops : for and while)
  • [4: python (3)-Data Structures](#4: python (3)-Data Structures)
  • [5: python (4)-Advanced feature & Philosophy](#5: python (4)-Advanced feature & Philosophy)
  • [6: Simple Practice Examples (Python)](#6: Simple Practice Examples (Python))
    • [🧩 Example 1: Odd or Even (if statement)](#🧩 Example 1: Odd or Even (if statement))
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 2: Sum with a Loop (for)](#🧩 Example 2: Sum with a Loop (for))
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 3: String Processing](#🧩 Example 3: String Processing)
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 4: Function (Basic)](#🧩 Example 4: Function (Basic))
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 5: List Processing](#🧩 Example 5: List Processing)
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 6: Dictionary (Counting)](#🧩 Example 6: Dictionary (Counting))
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 7: Simple Class (OOP)](#🧩 Example 7: Simple Class (OOP))
      • [✅ Solution](#✅ Solution)
    • [🧩 Example 8: File Writing](#🧩 Example 8: File Writing)
      • [✅ Solution](#✅ Solution)
  • [🎯 Quick Summary (for exams)](#🎯 Quick Summary (for exams))

1:introduction

people should constantly improve themselves.

I ofter use Python in my work and studies , However I have not systematically learned Python grammear before.

this is something I wangt to change

I aim to learn Python grammar thoroughly and gain a deeper understanding of it

2:computer setting development environment & Jupyter Notebook

(1):computer setting development environment

It is ver important to ensure that your computer settings and development environment are properly configured .

If the setup is in corret or complicated , Python may not work properly , and errors may occur

Therefore we need to make sure that our computer setings and development environment are correctly prepared

System Requirements

  • win : 10 /11
  • python 3.x

(2):Jupyter Notebook

  • Write code >>> Shift + Enter to run
  • Use print() to display output

is used for comments

3: python (1)-base

(1):Features of python

  • simple and readable syntax
  • Interpreted language(no compilation needed)
  • Use indentation instead of {}
  • Dynamically typed (no type declaration required)
  • supports object-oriented and functional programming
  • Rich ecosystem (NumPy,pandas , AI libraries)

(2):Variables

  • Naming : letters , numbers, underscore
  • case - sensitive
  • No type declaration needed
  • Undefined variables cause errors

(3):Basic Data Types

  • None (no value) Vaid
  • int (integer)
  • bool (True=1 / False=1)
  • float(real number)
  • str(string):strings are immutable

(4):Operators

  • { + - * / } basic operations
  • // floor division
  • % remainder
  • ** exponent

4: python (2)-loop

(1):control structures

if statement : Uses indentation instead of brackets

c 复制代码
if statement :
    return ...
elif statement :
    return ...
else:
    return ...

(2): Loops : for and while

(1)for with range() :

c 复制代码
for i in range(n):

(2)while loop

bash 复制代码
while statement :

(3)keywords:

break >> exit loop

continue >> skip iteration

4: python (3)-Data Structures

(1):List

  • mutable , flexible
  • supports slicing
c 复制代码
append()
insert()
pop()
remove()

(2):Tuple

  • immutable
  • used for ()

(3):Dictionary

  • key-value structure
c 复制代码
{'a':1, 'b':2}

(4):Set

  • No duplicates, unordered

5: python (4)-Advanced feature & Philosophy

(1):Advanced feature

List Comprehension

  • Concise way to create lists
c 复制代码
[e*e for e in x]

(2):Philosophy

  • readability is important
  • simple is better than complex
  • Explicit is better than implicit

6: Simple Practice Examples (Python)


🧩 Example 1: Odd or Even (if statement)

Task:

Check whether a number is odd or even.

✅ Solution

python 复制代码
x = 7

if x % 2 == 0:
    print("even")
else:
    print("odd")

🧩 Example 2: Sum with a Loop (for)

Task:

Calculate the sum from 1 to 10.

✅ Solution

python 复制代码
total = 0

for i in range(1, 11):
    total += i

print(total)  # 55

🧩 Example 3: String Processing

Task:

Count how many times "a" appears in a string.

✅ Solution

python 复制代码
s = "banana"

count = 0
for c in s:
    if c == "a":
        count += 1

print(count)  # 3

🧩 Example 4: Function (Basic)

Task:

Create a function that returns the square of a number.

✅ Solution

python 复制代码
def square(x):
    return x * x

print(square(5))  # 25

🧩 Example 5: List Processing

Task:

Multiply all elements in a list by 2.

✅ Solution

python 复制代码
x = [1, 2, 3, 4]

y = []
for n in x:
    y.append(n * 2)

print(y)  # [2, 4, 6, 8]

👉 Short version (list comprehension):

python 复制代码
y = [n * 2 for n in x]

🧩 Example 6: Dictionary (Counting)

Task:

Count how many times each number appears.

✅ Solution

python 复制代码
nums = [1, 2, 1, 3, 2, 1]

d = {}

for n in nums:
    if n not in d:
        d[n] = 1
    else:
        d[n] += 1

print(d)
# {1: 3, 2: 2, 3: 1}

🧩 Example 7: Simple Class (OOP)

Task:

Create a Student class and print the name.

✅ Solution

python 复制代码
class Student:
    def __init__(self, name):
        self.name = name

    def show(self):
        print(self.name)

s = Student("Tom")
s.show()

🧩 Example 8: File Writing

Task:

Write a line into a file.

✅ Solution

python 复制代码
with open("test.txt", "w", encoding="utf-8") as f:
    f.write("Hello Python")

🎯 Quick Summary (for exams)

  • if → condition
  • for → loop
  • def → function
  • list → multiple values
  • dict → counting
  • class → objects

相关推荐
神仙别闹6 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
Y幽谷客6 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
伞伞悦读6 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人7 小时前
Windows删除图标缓存
windows·python·pyqt
qq_5470261797 小时前
Python 变量和简单数据类型
python
智搜广告8 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo
IpdataCloud9 小时前
AI智能体调用工具怎么核验来源IP?归属地、网络类型与代理风险识别(含Python代码)
数据库·python·tcp/ip
2601_966949659 小时前
只拿到股票代码还不够:量化程序到底还需要哪些标的信息?——从数据建模开始理解股票元数据
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
蒸鱼Yuzheng9 小时前
Python 游戏测试开发怎么准备:日志解析、接口校验、并发与可维护性
自动化测试·python·测试开发·面试题·游戏测试
m0_7345717610 小时前
深入理解5g <十八>传输块tbsize的计算
开发语言·5g