Python 基础知识(1)

来源:互联网 发布:sqlserver 导出数据 编辑:程序博客网 时间:2024/06/15 10:25

Python基础

  • Python基础
    • 一变量
      • 1变量是用来存储数据对象的
      • 2英文定义
      • 3Python定义变量的规则
      • 4变量的赋值
    • 二 字符编码后续
    • 三用户输入输出
      • 1列表内容
      • 2input函数
    • 五数据类型
      • 1数字
      • 2布尔值
      • 3字符串
    • 六数据运算

一、变量

1、变量是用来存储数据对象的。


2、英文定义:

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.

3、Python定义变量的规则:

  • 变量名只能是字母、数字或者下划线的任意组合。
  • 变量名的第一个字符不能是数字。
  • 以下关键字为Python关键字,不能声明为变量。
    [‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

4、变量的赋值:

Python是动态语言,变量无需提前声明变量类型,Python变量通过赋值可以指向任意的类型or对象。


二、 字符编码(后续)


三、用户输入输出

1、列表内容

2、input()函数:

  • 在Python 3.x默认输入的都是字符串类型的数据。
>#name = raw_input("What is your name?") #only on python 2.x >name = input('What is your name')>print('Hello',+name)
  • getpass函数:
    在输入密码时,如果想要密码不可见,可调用getpass模块中的getpass函数。
>import getpass># 将用户输入的内容赋值给 name 变量>pwd = getpass.getpass("请输入密码:")># 打印输入的内容>print(pwd)

五、数据类型

1、数字:

  • int(整型):在32位机器上,整数的位数为32位,取值范围为-2**31~2**31-1,即-2147483648~2147483647。
    在64位系统上,整数的位数为64位,取值范围为-2**63~2**63-1,即-9223372036854775808~9223372036854775807
  • long(长整型):自从Python2.2起,如果整数发生溢出,Python会自动将整数数据转换为长整数。
    从Python3.0开始LONG(长整型)的概念就消失了,只有整型了

  • float(浮点型):

详细介绍

  • complex(复数):
    复数由实数部分和虚数部分组成,一般形式为x+yj,其中的x是复数的实数部分,y是复数的虚数部分,这里的x和y都是实数。
    注:Python中存在小数字池:-5 ~ 257

2、布尔值

真假(1或0)


3、字符串

“”Hello World“”


  • 万恶的字符串拼接:

python中的字符串在C语言中体现为是一个字符数组,每次创建字符串时候需要在内存中开辟一块连续的空,并且一旦需要修改字符串的话,就需要再次开辟空间,万恶的+号每出现一次就会在内从中重新开辟一块空间。

六、数据运算


更多内容:猛击这里

原创粉丝点击