python中done_python语言入门 第一天

初识Python

一、python介绍

- 解释器:

cpython(默认使用)

ipython(shell)

jpython(java)

ironpython

rubypython

- 编码:

ascii:用一个字节=8比特,标识计算机能表达的所有东西

unicode:万国码,用4个字节=32位来做对应关系

utf-8:针对万国码进行压缩,至少使用1个字节表示

gbk:针对亚洲国家的文字所做的对应关系

中文2字节=16位

注意:utf-8编码中中文占3个字节,gbk中中文占2个字节

归纳:

编码大小支持语言

ASCII

1个字节

英文

Unicode

2个字节(生僻字4个)

所有语言

UTF-8

1-6个字节,英文字母1个字节,汉字3个字节,生僻字4-6个字节

所有语言

- 版本:

python2:解释器默认编码:ascii

格式:# -*- coding:utf-8 -*-

python3:解释器默认编码:utf-8

格式:无需配置编码格式

- IDE:

- pycharm

- 文字大小

- 模板

- vim

- notepad

二、模块:

- time:

time模块提供各种时间相关的功能

与时间相关的模块有:time,datetime,calendar

- getpass:

加密输入密码

- os:

包含普遍的操作系统功能

三、语法:

- 输入、输出:

print:

print("你是疯儿他是傻")

input:

# 密文密码

# import getpass

# user = input("enter a user,please")

# passwd = getpass.getpass("enter a num,please")

# print(user,passwd)

- 变量:

格式:变量名 = 值

规范:

a.数字、字母、下划线

b.不能以数字开头

c.不能使用python关键字

建议:简明知意;user_pwd = "xxx"

示例:

name = 'anthony'

user = 'anthony'

- 数据类型:

age = 18 # 整数类型

name = "anthony" #字符串类型

列表:

user_list = ["紫薇","尔康","18","海量","小鸡"]

n3 = user_list[0]

n4 = user_list[1] # "尔康"

user_list = ["紫薇","尔康","18","海量","小鸡"]

for xxx in user_list:

print(xxx)

if xxx == '18':

break

字典:

user_info = {"name":"紫薇","age":18}

n5 = user_info["name"]

n6 = user_info["age"]

user_info['count'] = 666

# {"name":"紫薇","age":18,"count":666}

数据类型嵌套:

n7 = ["alex","eric",[11,22,33]]

n7[1]

n7[2][1]

n8 = [

"alex",

{'name':'日天','age':18},

[11,22,33]

]

n8[1]["age"] = 19

- 条件语句:

格式:

格式一:

if 条件:

成功之后走这里

格式二:

if 条件:

成功之后走这里

else:

失败之后走这里

格式三:

if 条件:

成功之后走这里

elif 条件:

成功之后走这里

elif 条件:

成功之后走这里

else:

上述都失败

示例:

# while True:

# # 定义类目

# msg = '''

# 欢迎来到大世界

# 1、可乐

# 2、雪碧

# 3、柠檬茶

# 4、退出

# '''

# # 打印类目

# print(msg)

# # 定义选项

# choice = input("请输入你选择的类目:")

#

# # 判断所选类目

# if choice == '1':

# print("1、伏特加\n2、葡萄酒\n3、白酒4、退出")

# # 定义酒品类目

# search_type = input("请输入你选择的酒品类目:")

# if search_type == '1':

# print("伏特加")

# elif search_type == '2':

# print("葡萄酒")

# elif search_type == '3':

# print("白酒")

# else:

# print("输入错误")

# break

# elif choice == '2':

# print("雪碧")

# elif choice == '3':

# print("柠檬茶")

# elif choice == '4':

# break

# else:

# print("输入错误")

- 循环语句:

格式:

while 条件:

条件成立执行

while True:

print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

while 1==1 and 2==2:

print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

timer = 0

while timer < 3:

print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

timer = timer + 1

print('完成')

示例:

# count = 1

# while count < 11:

# if count == 7:

# count +=1

# continue

# print(count)

# count +=1

- 循环退出:

- break:

定义:强制终止当前所在循环

示例:

示例一:

while True:

print('钓鱼要钓刀鱼,刀鱼要到岛上钓')

break

示例二:

页面上输出 1 - 10

a. count = 1

while count < 11:

print(count)

count = count + 1

b. count = 1

while True:

print(count)

count = count + 1

if count == 11:

break

c. count = 1

while True:

print(count)

if count == 10:

break

count = count + 1

- continue:

定义:跳出本次循环,继续下一次循环。

示例:

示例一:

a. count = 1

while count < 11:

if count == 7:

count = count + 1

continue

print(count)

count = count + 1

b. count = 1

while count < 11:

if count == 7:

pass

else:

print(count)

count = count + 1

- 循环练习:

a. 1-100的所有数的和

# 定义基数

sum = 0

count = 1

# 循环开始

while True:

if count == 100:

break

else:

count += 1

sum += count

print(sum)

b. 1-2+3-4+5 ... 99的所有数的和

sum = 0

for count in range(1,101):

if count % 2 == 1:

sum -= count

elif count % 2 == 0:

sum += count

print(sum)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值