Python入门-Day1

1.python初体验

print() 用于输出括号内的内容
input() 让用户输入内容

print('Hello')
print(1+1)
print('1+1')#加了引号表示输出的是引号内的字符串

2.基础讲解

变量

用于储存数据的(使用变量需先对变量赋值)
命名规则:
1.变量名必须为字母或下划线开头,后跟字母,下划线或数字
2.变量名不能使用python的关键字和函数名(例如True , False , None , is , del , if , felif , else , pass , not 等)
3.常量采用所有字母大写来定义
如何命名规范的变量名

注释方法

1.单行注释用 #
2.多行注释在开始和结尾用三个单引号或三个双引号表示

python中":"的作用

1.在分支语句和循环语句中表示换行
2.定义分片中的步长
[m : ] 代表列表中的第m+1项到最后一项
[ : n] 代表列表中的第一项到第n项

a=[0,1,2,3,4,5]
a[2:]=[3,4,5]
a[:2]=[0,1]
学会使用dir( )及和help( )

dir() 函数不带参数时,返回当前范围内的变量、方法和定义的类型列表;带参数时,返回参数的属性、方法列表。如果参数包含方法__dir__(),该方法将被调用。如果参数不包含__dir__(),该方法将最大限度地收集参数信息。
help()函数:用于查询函数或模块用途

>>> help(dir)
Help on built-in function dir in module builtins:

dir(...)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.
    Else, return an alphabetized list of names comprising (some of) the attributes
    of the given object, and of attributes reachable from it.
    If the object supplies a method named __dir__, it will be used; otherwise
    the default dir() logic is used and returns:
      for a module object: the module's attributes.
      for a class object:  its attributes, and recursively the attributes
        of its bases.
      for any other object: its attributes, its class's attributes, and
        recursively the attributes of its class's base classes.
import使用

import语句作用就是用来导入模块的,它可以出现在程序中的任何位置。
导入模块:import module(模块名)
导入多个模块:import module1,module2,module3…….
导入模块中的某个函数:from module import name

pep8介绍

Python PEP8 编码规范

3.python数值基本知识

六个标准数据类型:
不可变数据(3 个):Number(数字)、String(字符串)、Tuple(元组)
可变数据(3 个):List(列表)、Dictionary(字典)、Set(集合)
(1)整型:int 通常被称为整型或整数,是正、负整数,不带小数点。
(2)浮点数:float,由整数部分与小数部分组成
e记法(科学计数法):e不区分大小写1.2e2=120.0,1.2E-2=0.012

>>> 1.2e2
120.0
>>> 1.2E2
120.0
>>> 1.2e-2
0.012

(3)复数:由实数部分和虚数部分构成,表示为:a+bj 或 complex(a,b) 。复数的实部a 和虚部 b 都是浮点型
(4)bool:int 类型的子类,False 等同于0,True 等同于1

算数运算符

① + ② - ③ * ④/ ⑤% ⑥// ⑦**

从左到右依次是:加、减、乘、除、取余、取整、幂运算

print(a+b)#a和b相加
print(a-b)#a和b相减
print(a*b)#a和b相乘
print(a/b)#a除b
print(a%b)#a除b取余数
print(a//b)#a除b取整数
print(a**b)#a的b次方
逻辑运算符

and: x and y, 若x为False,返回x;若x为True,返回y;
or: x or y, 若x为True,返回x;若x为False, 返回y;
not: not x, 若x为True,返回False;若x为False,返回True。

成员运算符
运算符描述实例
in如果在指定的序列中找到值返回 True,否则返回 False。x 在 y 序列中 , 如果 x 在 y 序列中返回 True。
not in如果在指定的序列中没有找到值返回 True,否则返回 False。x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。
>>> a = 1
>>> b = 6
>>> list = [1, 2, 3, 4, 5 ];
>>> if ( a in list ):
...    print ("!")
... else:
...    print ("?")
...
!
>>> if ( b not in list ):
...    print ("!")
... else:
...    print ("?")
...
!
身份运算符
>>> a=1
>>> b=2
>>> a is b
False
>>> a is not b
True
>>> a=a+1
>>> a is b
True
运算符优先级(由高到低)
运算符描述
**指数
* / % //乘,除,取模和取整除
+ -加法减法
<= < > >=比较运算符
is is not身份运算符
in not in成员运算符
and or not逻辑运算符

4.string字符串

定义:字符串是 Python 中最常用的数据类型。我们可以使用引号('或")来创建字符串。

常见操作
>>> a='Let\'s go.'#使用转义字符
>>> print(a)
Let's go.
>>> b="Let's go"
>>> print(b)
Let's go
>>> a1='hello'
>>> a2='world'
>>> print(a1+a2)#连接两个字符串
helloworld
>>> print('今天\n天气\n不错。')#换行
今天
天气
不错。
字符串格式化问题

常用格式化符号

符号描述
%d格式化整数
%s格式化字符串
%c格式化字符及其ASCII码
%e用科学计数法格式化浮点数
print('My name is %s and my height is %d cm.'%('Lihua',175))
输出结果
My name is Lihua and my height is 175 cm.
作业
name = input('请输入您的姓名:')
sex = input('请输入您的性别:')
age = input('请输入您的年龄:')
print('您的姓名是:%s,您的性别是:%s,您是%d年出生的人'%(name,sex,2019-int(age))) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值