python的基础初始第二天

1.基础数据类型初始

1,数字类型,int,用于计算,+ ,- ,*, /,加,减,乘,除。在python2有整型和长整型之分(3344L),在python3 已经不区分了。

2,字符串类型string,在python中,被引号引起来的数据就是字符串。可以是单引号  '    '或双引号  ''   '',三引号'''  ''',引号必须是成对出现的。在PYthon中单引号和双引号的作用是一样的。三引号 主要是用于换行。

字符串可以与字符串用+,与字符串相加拼接。" aaa"+"bbbb",也可以与数字相乘。

>>> type(name),type(age2)
(<type 'str'>, <type 'int'>)
>>>
>>> name
'Alex Li'
>>> age2
>>> name + age2
Traceback (most recent call last):
File "<stdin>", line , in <module>
TypeError: cannot concatenate 'str' and 'int' objects #错误提示数字 和 字符 不能拼接
>>> name
'Alex Li'
>>> age
''
>>>
>>> name + age #相加其实就是简单拼接
'Alex Li22'
>>>
>>> name * #相乘其实就是复制自己多少次,再拼接在一起
'Alex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex LiAlex Li'

怎么区分在python中的数据类型。

>>> type(name),type(age2)
(<type 'str'>, <type 'int'>)
>>>
>>> name
'Alex Li'
>>> age2
>>> name + age2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cannot concatenate 'str' and 'int' objects #错误提示数字 和 字符 不能拼接

3,bool值 布尔值在计算机只有True 和 False,真和假之分。

>>> a=3
>>> b=5
>>>
>>> a > b #不成立就是False,即假
False
>>>
>>> a < b #成立就是True, 即真
True

4,用户交互(input)

只需要把要打印的格式先准备好, 由于里面的 一些信息是需要用户输入的,你没办法预设知道,因此可以先放置个占位符,再把字符串里的占位符与外部的变量做个映射关系就好啦

input输入的数据类型都是字符串数据类型。

name = input("Name:")
age = input("Age:")
job = input("Job:")
hobbie = input("Hobbie:") info = '''
------------ info of %s ----------- #这里的每个%s就是一个占位符,本行的代表 后面拓号里的 name
Name : %s #代表 name
Age : %s #代表 age
job : %s #代表 job
Hobbie: %s #代表 hobbie
------------- end -----------------
''' %(name,name,age,job,hobbie) # 这行的 % 号就是 把前面的字符串 与拓号 后面的 变量 关联起来 print(info)

在python中 %s就是代表字符串占位符,除此之外,还有%d,是数字占位符.如果把上面的age后面的换成%d,就代表你必须只能输入数字啦,否则会报错。

5,流程控制之     if 语句

假如把写程序比做走路,那我们到现在为止,一直走的都是直路,还没遇到过分叉口,想象现实中,你遇到了分叉口,然后你决定往哪拐必然是有所动机的。你要判断那条岔路是你真正要走的路,如果我们想让程序也能处理这样的判断怎么办? 很简单,只需要在程序里预设一些条件判断语句,满足哪个条件,就走哪条岔路。这个过程就叫流程控制。

if... .else 语句

if  +条件:

单分支

满足条件后要执行的代码。

"""
if 条件:
满足条件执行代码
else:
if条件不满足就走这段
"""
AgeOfOldboy = if AgeOfOldboy > :
print("Too old, time to retire..")
else:
print("还能折腾几年!")

缩进,

你会发现,上面的if代码里,每个条件的下一行都缩进了4个空格,这是为什么呢?这就是Python的一大特色,强制缩进,目的是为了让程序知道,每段代码依赖哪个条件,如果不通过缩进来区分,程序怎么会知道,当你的条件成立后,去执行哪些代码呢?

Python是门超级简洁的语言,发明者定是觉得用{}太丑了,所以索性直接不用它,那怎么能区分代码块呢?答案就是强制缩进。

Python的缩进有以下几个原则:

  • *代码必须顶行写,即如果一行代码本身不依赖于任何条件,那它必须不能进行任何缩进
  • 同一级别的代码,缩进必须一致
  • 官方建议缩进用4个空格,当然你也可以用2个,如果你想被人笑话的话。

多分支

回到流程控制上来,if...else ...可以有多个分支条件

if 条件:
满足条件执行代码
elif 条件:
上面的条件不满足就走这个
elif 条件:
上面的条件不满足就走这个
elif 条件:
上面的条件不满足就走这个
else:
上面所有的条件不满足就走这段
#一个猜年龄的游戏
age_of_oldboy = guess = int(input(">>:")) if guess > age_of_oldboy :
print("猜的太大了,往小里试试...") elif guess < age_of_oldboy :
print("猜的太小了,往大里试试...") else:
print("恭喜你,猜对了...")

6,流程控制之 -- while 循环

while 循环条件:

#循环体

如果条件为真,那么循环体则执行;

如果条件为假,那么循环体不执行.

循环终止语句:

如果在循环的过程中,因为某些原因,你不想继续循环了,怎么把它中止掉呢?这就用到break 或 continue 语句

  • break用于完全结束一个循环,跳出循环体执行循环后面的语句
  • continue和break有点类似,区别在于continue只是终止本次循环,接着还执行后面的循环,break则完全终止循环。
  • 例子
  • #break
count =
while count <= : #只要count<=100就不断执行下面的代码
print("loop ", count)
if count == :
break
count += #每执行一次,就把count+,要不然就变成死循环啦,因为count一直是0 print("-----out of while loop ------")

continue

count =
while count <= :
count +=
if count > and count < : #只要count在6-94之间,就不走下面的print语句,直接进入下一次loop
continue
print("loop ", count) print("-----out of while loop ------")

while  ....else 语句

与其它语言else 一般只与if 搭配不同,在Python 中还有个while ...else 语句

while 后面的else 作用是指,当while 循环正常执行完,中间没有被break 中止的话,就会执行else后面的语句

count =
while count <= :
count +=
print("Loop",count) else:
print("循环正常执行完啦")
print("-----out of while loop ------")
python的基础初始第二天

如果执行过程中被break啦,就不会执行else的语句啦

count =
while count <= :
count +=
if count == :break
print("Loop",count) else:
print("循环正常执行完啦")
print("-----out of while loop ------")

下面是一些小练习:

1,求1到100之间的和值。

count =
sum =
while count <:
count = count +
sum = sum + count
if count == :
print (sum)

2,使用while循环输入 1 2 3 4 5 6     8 9 10

count =
while count <:
count = count +
if count == :
continue
print(count)

3,输出 1-100 内的所有偶数

count =
while count <:
count = count +
print(count)

4,输出 1-100 内的所有奇数

count =
while count <:
if count%== :
pass
else:
print(count)

5,求1-2+3-4+5-6+.......+99的和值

sum =
number =
while number < :
if number% == :#取的奇数
sum += number
else:
sum -= number
number +=
print(sum)

6,用户输入三次

# _user = "yuyu"
# _passwd ="abc123"
# count = 0
# while count <3:
# username = input("Username:")
# password = input("Password:")
# if username == _user and password == _passwd:
# print("Welcome %s login..."% _user)
# break
# else:
# print("Invalid username or password!")
# count += 1
上一篇:第一次网页前端培训(HTML常用标签)


下一篇:聊聊kafka结构