Python学习笔记(一)

1. Python介绍

1.1Python基础语法

  1. 定义变量

    x=1
    y=2
    z=x+y

  2. 判断语句

    score=90
    if score>=90:
    print(“优秀”)
    elif score>=80
    print(“良好”)
    elif score>60
    print(“及格”)

  3. 循环语句

    For i in range(0,3)
    print(i)
    print(“end”)

  4. 定义函数def

    def HelloCNBlogs():
    print(“Hello cnblogs”)
    def GetMax(x,y):
    if x>y:
    return x
    else:
    return y
    HelloCNBlogs()
    print(GetMax(3,9))

  5. 面向对象class:

    class FirstTest:
    def init(self,name):
    self._name=name
    def SayFirst(self):
    print(“Hello {0}”.format(self._name))
    F = FirstTest(“CNBlogs”)
    F.SayFirst()

  6. 继承

    class FirstTest:
    def init(self,name):
    self._name=name
    def SayFirst(self):
    print(“Hello {0}”.format(self._name))

class SecondTest(FirstTest):
def init(self,name):
FirstTest.init(self,name)
def SaySecond(self):
print(“Good {0}”.format(self._name))

S=SecondTest(“CNBlogs”);
S.SayFirst()
S.SaySecond();

7. 引入其他类

# 第一种引入的方法
import FirstWork
#
S=FirstWork.SecondTest(“CNBlogs”);
S.SayFirst()
S.SaySecond();
第二种引入方法
from FirstWork import SecondTest
ST=SecondTest(“CNBlogs”);
ST.SayFirst()
ST.SaySecond();

1.2Python 控制流和函数

  1. python 基本数据
    整数(int)
    长整数(long)
    浮点数(float)
    布尔(Boolean)
    虚数(complex)

     虚数的使用:  
    


    print(complex(1))
    print(complex(‘3+89j’))
    print(complex(4,5))

1.3Python模块

 按需求类别将一些常用的功能(函数)组合在一起,形成模块。
 常用Python模块,一些需要下载
 Urllib 
 numpy\mkl
 pandas
 matpoltlib
 scipy
 stasmodels
 Gensim

1. 模块安装
模块库下载地址
调用cmd安装
命令: pip install 模块库名.whl
2. 导入模块
例如:

import urllib
from urllib import request
data3=request.urlopen(“http://baidu.com“).read()
print( len(data3))

2. Python文件操作

import os
os.getcwd() #获取当前目录地址

file=open(‘test.txt’,w) #默认的mode是‘r’
open用于打开文件,如果没有该文件,那么新建一个文件
file.read()

3.Python异常值处理

try:
print(“My”)
Except Exception as er:
print(er)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值