python基础入门

输入:

name=int(input('请输入名字:'))
请输入名字:

输出:

 print('{}网址: "{}!"'.format('百度', 'www.baidu.com'))
百度网址: "www.baidu.com!"

数组:

#左取右不取
str = 'HelloWorld!'
print("str:",str)  # 输出完整字符串
print("str[0]:",str[0]) # 输出字符串中的第一个字符
print("str[0:5]:",str[0:5] ) # 输出字符串中第三个至第六个之间的字符串
print("str[2:]:",str[2:])# 输出从第三个字符开始的字符串
print("str * 2 :",str * 2 ) # 输出字符串两次
print("str + \'TEST\':",str + "TEST" ) # 输出连接的字符串

str: HelloWorld!
str[0]: H
str[0:5]: Hello
str[2:]: lloWorld!
str * 2 : HelloWorld!HelloWorld!
str + 'TEST': HelloWorld!TEST

条件:

if age>=19 and sex=='男':
    print ('该上班了')
elif age<18 or sex=='女':
    print('上学吧还是')
elif not(sex=='男' and  sex=='女'):
    print('既不是男也不是女')
else:
   print("其他。")

循环:
for:

for letter in 'Python':     # 第一个实例
   print('当前字母 :', letter)
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # 第二个实例
   print ('当前水果 :', fruit)
当前水果 : banana
当前水果 : apple
当前水果 : mango
for i in range(1,5):
    print(i)
# 5取不到
1
2
3
4

while:

count = 0
while (count < 9):
   print ('The count is:', count)
   count = count + 1
The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8

跳出:
break

for letter in 'Python':  
   if letter == 'h': #不等于用!=表示
      break
   print( '当前字母 :', letter)
当前字母 : P
当前字母 : y
当前字母 : t

continue

for letter in 'Python':     
   if letter == 'h':
     continue
   print( '当前字母 :', letter)
当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : o
当前字母 : n

函数:

def fun(a, b): # 变量传值,列表传地址
    a = a+1
    b[0] = b[0]+1
    return " ok" # 返回

i = 0
j = [1]
print(fun(i, j), i, j[0], fun(i, j))
 ok 0 2  ok

字典:

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} #相当于类的属性和值
print(dict['Name'])
Zara

类:

class Employee:
    empCount = 0 # 相当于static属性
    def __init__(self, name, salary):#构造函数
        self.name = name
        self.salary = salary
        Employee.empCount += 1
    def displayCount (self):#类的方法
        print("Total Employee %d" % Employee.empCount)
    def displayEmployee (self):
        print("Name : ", self.name, ", Salary: ", self.salary)
        
e = Employee( "aas", 5 ) # new一个对象
print(Employee.empCount) # 打印静态变量
e.displayEmployee() # 调用对象的函数
1
Name :  aas , Salary:  5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值