python学习笔记——Python基础

一、python 背景                                                               

   1.python 主要应用领域

云计算

web开发 :django框架

科学计算、人工智能: 常用到的库有numpy 、pandas、matplotlib 等等

系统运维

金融

图形GUI

  2.应用Python较多的企业

        Google、豆瓣、知乎、Facebook

  3.Python语言的特点

        主要特点是:解释性、动态语言、强类型定义语言和弱类型定义语言。

         解释型:Python拥有良好的兼容性,在安装了解释器的条件下,可在不同平台运行,可以快速部署,但是每次运行都必须解释一遍,性能上不如编译型语言。

         动态语言:Python在运行期间去做数据类型检查 ,不用给任何变量指定数据类型。系统会在赋值变量的过程中,根据值的属性定义变量的数据类型。 

         强类型定义语言:在Python中,一旦一个变量被指定了某个数据类型,如果不经过强制转换,那么它将一直保持下去。

二、Python基本语法                             

  •  声明变量:变量名只能是字母。数字和下划线任意组合
  • 变量名的第一个字符不能是数字、
  • 不能使用关键字申明变量
#_*_coding:utf-8_*_
    
name = input("What is your name?")
print("Hello " + name )

    注意:Python2.*与Python3.* 代码风格有区别,

输入密码时,想隐藏输入结果,可利用getpass模块的getpass方法

import getpass

name = input("please input your name")
password = getpass.getpass("please input your password:")
print(name,password)
此处的代码不能再pycharm中直接运行,需要在dos界面进行验证


  1. 文件目录操作 :需要调用os模块 
  • 在当前路径下创建文件目录: 
os.mkdir('day1')  #只能生成一级目录

  • 如果想创建多级目录下文件,则需要调用makedirs()方法
os.makedirs('test111/test') 

  • 删除一级目录
os.rmdir('test111')  #此处删除目录下不能有文件

  • removedirs() 删除多级目录
os.removedirs('test111/test')  #此处不仅会删除文件test,还会删除test111文件夹
  • listdir() 显示当前目录下文件目录
print(os.listdir('.'))  #只会显示当前目录情况,不能显示子目录情况
print(os.listdir('/'))  #输出显示根目录下所有文件情况,不显示子目录

  • getcwd() 方法获取当前路径
print(os.getcwd())
  • chdir() 切换当前路径

os.chdir('/')   #切换到根目录下
print(os.getcwd())   #打印输出当前路径
  • 返回绝对路径方法
import os
def dirList(path):
     filelist =  os.listdir(path)
     filepath = os.getcwd()
     allfile = []
     for filename in filelist:
         allfile.append(filepath+"\\"+filename)

     return allfile


def dirList1(path):
    filelist = os.listdir(path)
    filepath = os.getcwd()
    allfile = []
    for filename in filelist:
        filepath = os.path.join(filepath,filename)
        allfile.append(filepath)
    return allfile
allfile1 = dirList1('test315')
allfile = dirList('test315')
print(allfile1,allfile)


猜你喜欢

转载自blog.csdn.net/zjt980452483/article/details/79573820