JAX study notes[9]

  1. notation
# 单行注释
  1. variable
x = 10  # 整数
y = 3.14  # 浮点数
name = "Alice"  # 字符串
  1. the number type
# 数字
int_num = 10
float_num = 3.14

# 字符串
str_val = "Hello, World!"

# 列表
list_val = [1, 2, 3, 4, 5]

# 元组
tuple_val = (1, 2, 3, 4, 5)

# 集合
set_val = {1, 2, 3, 4, 5}

# 字典
dict_val = {"name": "Alice", "age": 25}
  1. control flow
# if 语句
if x > 10:
    print("x 大于 10")
elif x == 10:
    print("x 等于 10")
else:
    print("x 小于 10")

# for 循环
for i in range(5):
    print(i)

# while 循环
while x < 10:
    x += 1
    print(x)
  1. function
# 定义函数
def greet(name):
    return f"Hello, {name}!"

# 调用函数
print(greet("Alice"))

# 带默认参数的函数
def greet(name="World"):
    return f"Hello, {name}!"

# 调用函数
print(greet())
print(greet("Alice"))
  1. class and object
# 定义类
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def greet(self):
        return f"Hello, my name is {self.name} and I am {self.age} years old."

# 创建对象
person = Person("Alice", 25)

# 调用方法
print(person.greet())
  1. handle the exception
    try:
    result = 10 / 0
    except ZeroDivisionError:
    print(“除数不能为零”)
    finally:
    print(“执行完毕”)

  2. moudle and package

# import moudle
import math

using function from moudle

print(math.sqrt(16))

import the special funtion

from math import sqrt

apply the function imported from the library

print(sqrt(16))
  1. control the file
# open the file
file = open("example.txt", "r")

# read the file 
content = file.read()
print(content)

# close the file
file.close()

write into file

file = open("example.txt", "w")
file.write("Hello, World!")
file.close()
  1. the standard library

using os module

import os

# to ask for the current work directory

```python
print(os.getcwd())

using sys module

import sys

to get the arguments of sys

print(sys.argv)

references

https://www.python.org/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海边的水水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值