Python入门到放弃系列二

本文介绍了Python的基础知识,包括列表操作、排序、逆置、条件判断、字典使用、用户输入、while循环和函数应用。通过示例展示了如何创建、修改、遍历和操作列表,以及如何使用if条件语句、字典的增删改查,以及用户交互和while循环控制流程。此外,还探讨了函数的定义和调用,以及导入和使用外部模块的方法。
摘要由CSDN通过智能技术生成

目录

 

第一部分 基础知识

第三章 列表简介

排序,逆置

第四章 操作列表

第五章 if

第六章  字典

第七章 用户输入和while循环

while 循环

第八章  函数


第一部分 基础知识

第三章 列表简介

直接脚本写代码了

bicycles=['trek','test','broke'] #列表?数组
print(bicycles) #默认打印整个列表
print(bicycles[1].title())  #指定下标元素 ,索引从0开始-和java一样的。
#修改元素
bicycles[1]='yamaha'  #修改元素
print(bicycles)
bicycles.append('feige') #增加元素
print(bicycles)
bicycles.insert(0,'yadi') #插入元素
print(bicycles)
bicycles.__delitem__(0)  #语法上可以使用  del bicycles[0]来删除
print(bicycles)
newVar=bicycles.pop()  #取出最后一个,也可以:bicycles.pop(0)指定位置
print(newVar)
print(bicycles)
bicycles.remove('broke'); #删除指定值得元素
print(bicycles)

 

输出打印:

['trek', 'test', 'broke']

Test

['trek', 'yamaha', 'broke']

['trek', 'yamaha', 'broke', 'feige']

['yadi', 'trek', 'yamaha', 'broke', 'feige']

['trek', 'yamaha', 'broke', 'feige']

feige

['trek', 'yamaha', 'broke']

['trek', 'yamaha']

[Finished in 0.0s]

 

排序,逆置

 

motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.sort()  # A-Z排序,改变了列表本身
# del motorcycles[0]; #测试删除
print(motorcycles)
motorcycles = ['honda', 'yamaha', 'suzuki']
newlist = sorted(motorcycles)  # 不影响老的列表,不改变列表本身
print("new:"+str(newlist))
print("old:"+str(motorcycles))
motorcycles.reverse()  # 倒着打印,逆置本身
print(motorcycles)
print(len(motorc

输出:

['honda', 'suzuki', 'yamaha']

new:['honda', 'suzuki', 'yamaha']

old:['honda', 'yamaha', 'suzuki']

['suzuki', 'yamaha', 'honda']

3

[Finished in 0.0s]

 

第四章 操作列表

magicans = ['alice', 'david', 'carolina']
for name in magicans:  # 循环打印元素  for循环中的多个指令依靠格式
    print(name)
    print(name.title()+"-second print")

#换了格式行后执行for之外命令
print("----yes, the end!-----")
#数值列表
numList=list(range(1,10))
suquars=[] 
for num in numList:
    print(num)
    suquars.append(num ** 2)

print(min(suquars))  #取最小 or 最大
print(max(suquars))

splitA=suquars[0:3]  #取前3个元素 下标:0~2  切片
print(splitA)
print(numList[1:4])  #取从1开始第四个 取 下标:1~3
print(numList[7:]) #取从下边7以后得
copyList=numList[:]  #复制列表  
#注意点:如果直接赋值给copyList后,其中一个改变俩都会改变值,而现在这种不影响
print(copyList)
#----元组
tupleA=(200,300)
print(tupleA)  
print(str(tupleA[0])+"-"+str(tupleA[1])) #取元组对应值
for x in tupleA:
    print(x)  #打印元组属性

tupleA=(100,400) #修改元组值
print(tupleA)

输出结果:

alice

Alice-second print

david

David-second print

carolina

Carolina-second print

----yes, the end!-----

1

2

3

4

5

6

7

8

9

1

81

[1, 4, 9]

[2, 3, 4]

[8, 9]

[1, 2, 3, 4, 5, 6, 7, 8, 9]

(200, 300)

200-300

200

300

(100, 400)

[Finished in 0.0s]

 

几分钟完成示例,还是挺简单的,注意点:注意格式和中英字符问题,其他的就是语法问题了。

第五章 if

cars = ['A', 'B', 'C', '大灵王']
''' 
if -elif-else 结构
'''
for x in cars:
    if x == '大灵王':  # if : 语法 判断   #elif :
        print('i am dalingwang!')
    elif x == 'B':  # elif :
        print('B ....')
    else:
        print('other...')
car = 'bmw'
print(car == 'bmw')  # 真值
print(car == 'BMW')  # 假值
print('not equal:' + str(car != 'A'))  # 否定判断
numA = 123
print(numA > 100 and numA < 124)  # 判断数字 ,支持多条件 and 和or 连接
print(numA < 100 or numA > 124)
print('A in list:'+str('A' in cars))  # in集合
print('D not in list:'+str('D' not in cars))  # not In集合

输出:

other...

B ....

other...

i am dalingwang!

True

False

not equal:True

True

False

A in list:True

D not in list:True

[Finished in 0.1s]

第六章  字典

初步理解字典就是key-value对象信息(封装成一个json格式的对象?可以通过对应访问属性方式(或者key)来访问)

alien_0 = {'color': 'green', 'pionts': 5}  # 创建字典-也可以 xxx={}
print(alien_0['color'])
print(alien_0['pionts'])
print(alien_0)
alien_0['x_pos'] = '0'  # 插入新值
alien_0['y_pos'] = '26'
alien_0['y_pos'] = '29'  # 重复属性会被覆盖
alien_0['user_0'] = {'name': 'nA', 'age': '18'}  # 支持嵌套对象
print(alien_0)
del alien_0['x_pos']  # 删除属性
print(alien_0)
for key, value in alien_0.items():  # 循环key-value
    print(str(key)+"----"+str(value))
print(alien_0.keys())
print(type(alien_0.keys()))  # 查看keys()返回的数据结构类型

输出:

green

5

{'color': 'green', 'pionts': 5}

{'color': 'green', 'pionts': 5, 'x_pos': '0', 'y_pos': '29', 'user_0': {'name': 'nA', 'age': '18'}}

{'color': 'green', 'pionts': 5, 'y_pos': '29', 'user_0': {'name': 'nA', 'age': '18'}}

color----green

pionts----5

y_pos----29

user_0----{'name': 'nA', 'age': '18'}

dict_keys(['color', 'pionts', 'y_pos', 'user_0'])

<class 'dict_keys'>

[Finished in 0.0s]

 

第七章 用户输入和while循环

在sublime 中使用 input函数需要安装一个插件,参考:https://www.cnblogs.com/8023spz/p/9511797.html

input()函数使用,

有人解释 input在python2和python3用法不太一样,反正输入记得要带上引号信息  ,https://www.zhihu.com/question/31388311

以下实验,

代码:

msg = input('pls input your name:')
print('hello:' + msg + '!')
age = input('pls input your age:')
print(int(input('pls input your number:')))

安装好插件执行,F5后,记得控台输入的时候要引号

while 循环

number = 10
while number > 0:
    print(number)
    number = number-1  # 上网查了下不支持 --或者++,支持 number+=1 or number-=1
    number -= 1

输出:

10

8

6

4

2

[Finished in 0.0s]

示例2:

prompt = 'pls input some msg, and i will repeat it back to you,'
prompt += "\n enter 'quit' to end!:"
msg = ''
while msg != 'quit':
    msg = input(prompt)
    print(msg)

print('end!!!!’)

输出:

pls input some msg, and i will repeat it back to you,

enter 'quit' to end!:'A'

A

pls input some msg, and i will repeat it back to you,

enter 'quit' to end!:'B'

B

pls input some msg, and i will repeat it back to you,

enter 'quit' to end!:'quit'

quit

end!!!!

 

***Repl Closed***

while循环中可以使用 标记  while  标记:xxx 就是标记成为变量的方式,而且也支持 break,和continue 语法。太简单懒得写了。

 

第八章  函数

就是封装成单个使用方法可以复用,或者封装,便于管理,各种语言大同小异,直接写代码

fun.py 文件内容:

def test_name(username, age, num):  # 带参数的函数
    print("hello:"+username+",age:"+age+",num:"+str(num))


test_name('ruishen', '10', 20)


def test_default(username='ruishen'):  # 默认值用法
    print("myname is :"+username)


test_default()  # 有默认值可以就不传这个值
test_default('ruishen hou')
test_default(username='Null-Name')  # 指定参数名来传递


def test_post(username, age):
    print("test post name is:"+username+",age:"+age)


test_post(age='18', username='ruishen')  # 支持不用按照指定位置,指定变量来传递,


def test_ret(username):  # 支持直接返回值返回
    return username+"-hello!-ret"


print(test_ret('ruishen'))


def test_ret_dict(): #测试返回 Map集合(字典)
    dict = {'key1': 123, 'key2': 'hou'}
    return dict;
varMap=test_ret_dict();
print("ret-Map:"+str(varMap))
 
def test_modify_param(dict):  #函数中修改入参信息 ,若担心可能执行中被修改原始的字典建议 取  [:]
    dict['newKey']='after add'
test_modify_param(varMap)
print("ret-Map2:"+str(varMap))

def test_compatible(*listParams):  #可变动态任意参数,和java中的三个点(...)相似
    print('I’m compatble param:'+str(listParams))
test_compatible(1,'hou','ruishen')
test_compatible(1,'ruishen’)

输出:

hello:ruishen,age:10,num:20

myname is :ruishen

myname is :ruishen hou

myname is :Null-Name

test post name is:ruishen,age:18

ruishen-hello!-ret

ret-Map:{'key1': 123, 'key2': 'hou'}

ret-Map2:{'key1': 123, 'key2': 'hou', 'newKey': 'after add'}

I’m compatble param:(1, 'hou', 'ruishen')

I’m compatble param:(1, 'ruishen')

[Finished in 0.0s]

 

fun_one.py 文件(用来测试 import使用的)

def test_abc():
    print('i am  test_abc')


def test_cdf():
    print('i am test_cdf')


new_import_fun.py文件内容如下
import fun #引入模块(直接写文件名即可)由于很测试代码都输出了
fun.test_name('ruishen', '10', 20)
from fun_one import test_abc  #引入指定模块的指定方法 多个用点隔开 a,b,c  如果 用 * 就是导入所有函数
test_abc() #直接调用引入的函数
from fun_one import test_abc as test1,test_cdf as test2  #别名用as  
test1()
test2()

输出:

hello:ruishen,age:10,num:20

myname is :ruishen

myname is :ruishen hou

myname is :Null-Name

test post name is:ruishen,age:18

ruishen-hello!-ret

ret-Map:{'key1': 123, 'key2': 'hou'}

ret-Map2:{'key1': 123, 'key2': 'hou', 'newKey': 'after add'}

I’m compatble param:(1, 'hou', 'ruishen')

I’m compatble param:(1, 'ruishen')

hello:ruishen,age:10,num:20

i am  test_abc

i am  test_abc

i am test_cdf

[Finished in 0.0s]

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值