美文网首页
Python 学习Day1

Python 学习Day1

作者: 快点学 | 来源:发表于2018-03-01 16:29 被阅读10次

  • Python 适合开发的领域

  • 基本操作

  • 函数

  • 文件操作

  • 参考


一、Python适合开发的领域


  • Web网站和各种网络服务
  • 系统工具和脚本
  • 作为“胶水”语言把其他语言开发的

二、基本操作

1、字符串处理


t = "He is a string. Who are you?";
print(t.capitalize());  # 将字符串的第一个字符变成大写,其余的变成小写
print(t.split());  # 分词
print(t.find('i'));  # 返回第一个i的下标
print(t.find('in'));  # 返回第一个in的下标
print(t.find('Python'));  # 未找到时返回-1
print(t[0:4]);  # 返回字符串中下标0-到3的字符
print(t.replace(' ', '|'))  # 将所有空格用|代替
2、List及Tuple的用法

l = [1, 2, 3.14, 'data']
print(type(l));
m = (1, 2, 3.14, 'data')
print(type(m))
l.append([4, 3])
print(l)
l.append(m)
print(l)
l.insert(3, 'beta')
print(l)
l.remove('data')
print(l)
l.remove(m)
print(l)
3、值与地址的传递

# Python 里的 = 传递的是地址
x = [1, 2, 3, 4]
y = x
y[0] = 5
print(x)
# Python 中使用copy方法来实现值的拷贝
x = [1, 2, 3, 4]
z = x.copy()
z[0] = 5
print(x)
print(z)
4、多维表

# 多维表
a = [[1, 2, 3, 4], [1, 2, 3, 4, 5], [1, 2, 3]]
print(a)
print(a[0][3])  # 第一个子表的第四个值
5、if else

a = [24, 16, 54]
b = []
if a[0] > a[1]:
    if a[0] < a[2]:
        b.append(a[0])
        if a[1] < a[2]:
            b.append(a[1])
            b.append(a[2])
        else:
            b.append(a[2])
            b.append(a[1])
print(a)
print(b)
6、range()

for i in range(2, 10, 3):  # 2到10中从2开始每个相隔3的数,前闭后开
    print(i)
    l = i ** 2
    print(l)

a = 0
sumup = 0
while a < 100:
    a += 1
    sumup += a
print(sumup)
7、break & continue

for i in range(300, 351):  # default step of range is 1
    if i % 17 == 0:
        print(i)
        break
    else:
        print("number")
        continue

三、函数


单返回值:

# Functions
def MaxOfTwo(x1, x2):
    if x1 >= x2:
        return x1
    else:
        return x2


a = 1
b = 2
c = MaxOfTwo(a, b)
print(c)

默认参数:

# Default arguments of function
def MinOfThree(x1=5, x2=10, x3=1):
    if x1 < x2:
        if x1 < x3:
            return x1;
        else:
            return x3;
    elif x2 < x3:
        return x2
    else:
        return x3

print(MinOfThree())

多返回值:

# Fuction returns max and min

def MaxMinThree(x1 = 19, x2 = 6, x3 = 7):
    return x1, x2

a1, b1 = MaxMinThree()
print(a1)
print(b1)

四、文件操作



1、'W'

file = open("newfile.txt", 'w')
file.write("I am created for the course . \n")
file.write("How about you? ")
file.write("How is your exam?")
file.close()
1、'W'

file = open("newfile.txt", 'w')
file.write("I am created for the course . \n")
file.write("How about you? ")
file.write("How is your exam?")
file.close()
2、'r'

file = open('newfile.txt', 'r')


# show whole file
print(file.read())
# the pointer of the file arrived end of the file, so the results of the functions below are null
file.seek(0)
# show first ten characters
print(file.read(10))
file.seek(0)    # Same reason like before
# view by line
print(file.readline())  # view first line
file.seek(0)    # Same reason like before
# view all by line
print(file.readlines())     # return lines list

file.close()

每次读取一行:

file = open("newfile.txt", 'r')
for line in file:
    print(line)
file.close()
1、'a'

file = open("newfile.txt", 'a')
file.write("\nI am back again.\n")
file.write("Do you miss me?\n")
file.close()
file = open("newfile.txt", 'r')
print(file.read())
file.close()

参考


相关文章

网友评论

      本文标题:Python 学习Day1

      本文链接:https://www.haomeiwen.com/subject/lgdgxftx.html