1.1 python基础语法

1. if语句

# coding=utf-8
#基础语法:首行缩进=大括号
if True:
    print("正确")
else:
    print("错误")

 if a>1 and b<2:
    print("hao")
else:
    print("bu")
    
#小例子
# int()向下取整
age=int(input("input your age:"))
if age<0:
    print("you are so cute")
elif age==3:
    print("are you kidding?")
else:
    print("nice!")
print("your age is:",age)
input("按确认键退出")

2. 变量

# 变量
# python在赋值过程中声明,且无分号
a=1
b=2.22
c="hello"
d=True
print(type(a))
print(type(b))
print(type(c))
print(type(d))

3. 5种数据类型

#5种数据类型
#数字
a1=100
a2=100.001
print(type(a2))

# 字符串
b1="hello,python!"
print(b1)
print(b1[5])
print(b1[4:8]) #得到5,6,7

#字符串拼接
b2="+++++ haha"
print("a said:"+b1+"   b said:"+b2)

#格式化
print("%d"%(23333))
print("%s"%("I'm gelute"))

4.列表List

#列表List
list1=[2333,"hahahha","哈哈哈",True]
list1.append("new")
# list1.remove(2333)
del list1[1]
list1[0]="ddd"
print(list1)
print(list1[2])
print(list1[1:3]) #1,2

#组合列表
list2=[1,2,3]
print(list1+list2)
list1.append(list2)
print(list1)
listSum=list1+list2
print(list1)

#判断是否在列表中
l2=4
if l2 in list2:
    print("在!")
else:
    print("求您再去别处找找吧")

#遍历列表
for item in [1,2,3,4,5,]:
    print("这里面有:",item)

5.元祖

#元祖:有序、内部数据不可修改
tup1=("钢铁侠","蜘蛛侠","奇异博士")
tup2=("洛基","灭霸","灭霸的头号小弟")
print(tup1)
print(tup1[1])
print(tup1[0:2])
tupSum=tup1+tup2
print(tupSum)
print(len(tupSum))

6.字典

#字典:无序,可以存储任意数据的对象(key:value>
dicl={"name":"张若昀","sex":"man"}
print(dicl["name"])
dicl["name"]="华晨宇"
print(dicl)
# del dicl["name"]
# print(dicl)
# del dicl
# print(dicl)
# dicl.clear()#清空,并非删除
# print(dicl)
dicKey=dicl.keys()#返回所有的key值
print(dicKey)
dicValue=dicl.values()#返回所有的value值
print(dicValue)

7.运算符

#运算符

#算术运算符
a=4
b=2
c=a**b #次方
print(c)
c=a//b #向下取整
print(c)

# 比较运算符 ><=

#赋值运算符,无c++ c--
a += b
print(a)

8.循环语句

#循环语句
#whie
count=0
while (count<9):
    print("The count is:",count)
    count +=1


# while True:
#     print("true")


# continue 、break
i=1
while i<10:
    i +=1
    if i%2>0:
        continue
    print(i)


#while..else
num=0
while num<10:
    num +=1
else:
    print("num超出范围!")


# for...in
hero=["0","1","2","3","4"]
for item in hero:
    print(item)
    # pass
    #此功能未实现,使用pass忽略其过程,保证其他顺利进行


#range()内置函数,生成数列
#range(start,end(不包含),step:1)
for i in range(0,5):
    print(i)


#len()
for i in range(0,len(hero)):
    print((hero[i]))


9.函数

#函数

#封装函数
def getName():
    name = "罗云熙"
    print(name)
    pass
    
    
#调用
getName()


def setInfo(name,sex,age):
    print(name,sex,age)
    pass


setInfo("杨浩","男",16)
#关键字传参,参数顺序改变
setInfo(sex="女",name="我不是杨浩",age=18)


#不定长传参
def Friends(first,second,*other):
    print("第一个朋友:",first)
    print("第二个朋友:", second)
    print("第其他朋友:", other)
    pass


Friends("晨晨","罗云熙","华晨宇","肖战")

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值