Python基础学习(一)

本文详细介绍了Python的基础知识,包括输入输出操作、条件判断、循环结构、数据类型及其操作,如列表和字典的增删查改,字符串处理,以及函数定义等核心概念。

Python基础学习

1、输入输出

input() #输入 ()里面可以加文字

print() #输出  '''三引号''' 代表自动换行输出   注:三引号还可以用于多行注释

EX:

name = input();
print(name)

print('''666
         666
         666
         666''')

 

2、判断

if 条件  :

     逻辑

elif 条件 :

     逻辑

else :

    逻辑

EX:

fenshu = 80
if fenshu >=80:
    print("优秀");
elif fenshu >=60 and fensh<80 :
    print("良好");
else:
    print("不及格")

3、循环

while(条件):

   循环内容

 

for  i in rang(0,4,1) :      ## 参数1代表起始值 参数2代表结束值 参数3代表步长

    循环内容

EX:

i= 10
while i>0:
    print(i)
    i= i-1

for j in range(1,10,1):
    print(j)

4、数据类型

列表list  操作: ##有序 用[  ]     元组用( )  里面的元素不可变和字符串一样

1、添加  :

+

append()

insert()

EX:

cat = ['tomu','kkk','gougou']
cat1,cat2,cat3=cat
print(cat1,cat2,cat3)

dog = ['dog1','dog2']
shop = dog+cat   
shop.append("dog3")  #添加到末尾 和加的操作是一样的
shop.insert(0,'dog0')  #可添加到任意位置
print(shop)

2、删除:

remove()

del 

EX:

shop.remove('dog3')  #删除列表中一个dog3
  
del shop[0:1]  和字符串切片一样
del shop[:]  #删除整个列表

3、查询:

in    

not in 

index()

'hello' in ['hello','hh','hhh','hhhh']  #返回True

test =  ['hello','hh','hhh','hhhh'] 
test.index('hello') #返回索引值 0

4、排序:

sort()

shop = [1,3,4,2]  #排序需要列表里的类型一致
shop.sort()   # 默认排序是从小到大 顺序
shop.sort(reverse=True) # reverse=True 是从大到小 逆序
shop.sort(key = str.lower) # 按ASCII字符排序  

5、其他

len() 列表长度

copy() 

deepcopy()

import copy
len(shop)  ##  shop的长度
shop1 = copy.copy(shop)  ##复制shop给shop1

 

字典操作:  #key {}  无序的

1、添加

setdefault() 

cat = {'name':'TT','color':'blue','age':12}

cat.setdefault('addr','sanming')  ##参数1是key 参数2是values 

2、查看

keys()

values()

item()

get()

cat = {'name':'TT','color':'blue','age':12}
for i in cat.values():  ##得到values值
    print(i)
for i in cat.keys():    ##得到key值
    print(i)
for i in cat.items():   ##得到整个字典值
    print(i)
print(cat.get('name','gg')) ##参数1是key值 如果存在输出字典key对于的values 
                            ##如不存在输出参数2设置的值

5、字符串操作

1、查询 in  

2、切片  [ : ]

3、大小写

upper()   lower() 

isupper()    islower()  

str = 'Love'

str1= str.upper()  ##全部大写
str2= str.lower()  ##全部小写
str1.isupper()  ##检测是否 都为大写 是输出True  否则输出Flase
str2.islower()  ##检测是否 都为小写 是输出True  否则输出Flase
print(str1+"   "+str2)

isalpha()  检测字符串是否只包含字母  

isalnum() 检测字符串是否只包含字母和数字

isdecimal()  检测字符串是否只包含数字

isspace()   检测字符串是否只包含空格 、制表符、换行

istitle()  检测字符串是否只包含以大写字母开头、后面都是小写的单词

##用法与上面的一样 

4、检测开头与结尾

startwith()

endwith()

filename = "test.txt"
boolen = filename.endswith(".txt")  #检测结尾  正确返回True 


url = "http://124568754531"
boolen1 = url.startswith("http:") #检测开头  正确返回True

5、合并与分割

join()

split()

part = ['i' ,'love' ,'you']
str1 = ' '.join(part); ##结果 i love you

'abc'.split('b') ## 结果 ['a','c']

6、文本操作

rjust()

ljust()

center()

print('hello'.rjust(20,'*'))   ## ***************hello
print('hello'.ljust(20,'*'))   ## hello***************
print('hello'.center(20,'*'))  ## *******hello********

strip()  #删除字符中多于的 字符

rstrip()  #删除右边字符中多于的 字符

lstrip()   #删除左边字符中多于的 字符

print('***************hello'.lstrip('*'))
print('hello***************'.rstrip('*'))
print('*******hello********'.strip('*'))

6、函数

def name() # def 定义函数  

def test(name):
    print('hello'+name)
  
print(test("python")) 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码敌敌畏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值