Python零基础入门(一)——Python基础关键字和语法

目录

1. Hello World!
2. 字符串操作
3. 简单数学计算
4. if elif else
5. 循环

基础类型
python中的主要基本数据类型是数字(整数和浮点数),布尔值和字符串

Hello World!

print('Hello World!')

Hello World!

单引号、双引号三引号:

print('Hello World!')
print("Hello World!")
print('''Hello World!''')

Hello World!
Hello World!
Hello World!

区分单引号、双引号三引号:
(三引号让程序员从引号和特殊字符串的泥潭里面解脱出来,自始至终保持一小块字符串的格式是所谓的WYSIWYG(所见即所得)格式的。)

\ 转移符,如:\n 换行、 \t 空格

print("The \n makes a new line")

The
makes a new line

print("The \t is a tab")

The is a tab

print('I\'m going to the movies')

I’m going to the movies

print("This is a string enclosed by \"\" not '' ")

This is a string enclosed by “” not ‘’

变量存储:

firstVariable = 'Hello World'
print(firstVariable)

Hello World

字符串操作

字符串是python的特殊类型。作为对象,在类中,您可以使用.methodName()表示法调用字符串对象上的方法。字符串类在python中默认可用,因此您不需要import语句即可将对象接口用于字符串。

print(firstVariable.lower())
print(firstVariable.upper())
print(firstVariable.title())

hello world
HELLO WORLD
Hello World

firstVariable.split(' ')

[‘Hello’, ‘World’]

a=firstVariable.split(' ')
a

[‘Hello’, ‘World’]

' '.join(a)

‘Hello World’

print("0" + "1")

01

"0" * 3

‘000’

"Fizz" + "Buzz"

‘FizzBuzz’

简单数学计算

有四种不同的数字类型:普通整数,长整数,浮点数和复数。另外,布尔值是普通整数的子类型。
(区分一些细小差别)

1+1

2

130-2.0

128.0

130-2

128

130/2

65.0

130.0/2

65.0

2*3

6

2**3

8

9%3

0

if 语句

比较操作符 功能
< 小于
<= 小于或等于
> 大于
>= 大于或等于
== 等于
!= 不等于
num = 3
if num == 3: print(num)

3

num = 3
if num > 10:print(num)
num = 3
if num % 3 == 0:print("Fizz")

Fizz

num = 10
if num % 5 == 0:print("Buzz")

Buzz

if True:print("This was True")

This was True

if False: print("Nothing printed")

逻辑操作符 描述
and 如果两个操作数均为True,则condition变为True.
or 如果两个操作数中的任何一个为True,则condition变为True.
not 用于反转逻辑(不是False变为True,而不是True变为False
num = 4
num > 0 and num  < 15

True

if num > 0 and num  < 15:print(num)

4

num = 4
num > 0 or num  > 15

True

if num > 0 or num  > 15:print(num)

4

if False or False:print('Nothing will print out')
num = 10
not num < 20

False

else 语句

num = 1
if num > 3 :print("Hi")
else: print("number is not greater than 3")

number is not greater than 3

num = 4
if num > 3 :print("Hi")
else: print("number is not greater than 3")

Hi

elif 语句

必须在if语句之后。 elif语句语句允许您检查True的多个表达式,并在其中一个条件求值为True时立即执行代码块。

与else类似,elif语句是可选的。但是,与其他情况不同,最多只能有一个语句,if后面可以有任意数量的elif语句。

my_num = 5
if my_num % 2 == 0:print("Your number is even")
elif my_num % 2 != 0:print("Your number is odd")
else: print("Are you sure your number is an integer?")

Your number is odd

dice_value = 5
if dice_value == 1:print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 2:print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 3:print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 4:print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 5:print('You rolled a {}. Great job!'.format(dice_value))
elif dice_value == 6:print('You rolled a {}. Great job!'.format(dice_value))
else:print('None of the conditions above (if elif) were evaluated as True')

You rolled a 5. Great job!

循环

For 循环 While 循环
遍历一组对象 条件为false时自动终止
没有break也可以结束 使用break语句才能退出循环

For 循环

For循环是迭代对象元素的常用方法

具有可迭代方法的任何对象都可以在for循环中使用。

python的一个独特功能是代码块不被{} 或begin,end包围。相反,python使用缩进,块内的行必须通过制表符缩进,或相对于周围的命令缩进4个空格。

#取第一个列表成员(可迭代),暂称它数字(打印它)
#取列表的第二个成员(可迭代),暂时将其称为数字,等等…

for number in [23, 41, 12, 16, 7]: print(number)
print('Hi')

23
41
12
16
7
Hi

枚举

返回一个元组,其中包含每次迭代的计数(从默认为0开始)和迭代序列获得的值:

friends = ['steve', 'rachel', 'michael', 'adam', 'monica']
for index, friend in enumerate(friends):print(index,friend)

0 steve
1 rachel
2 michael
3 adam
4 monica

Continue

continue语句将转到循环的下一次迭代

continue语句用于忽略某些值,但不会中断循环

cleaned_list = []for word in text.split(' '): if word == '':continuecleaned_list.append(word)
cleaned_list[1:20]

[‘a’,
‘dark’,
‘desert’,
‘highway’,
‘cool’,
‘wind’,
‘in’,
‘my’,
‘hair’,
‘Warm’,
‘smell’,
‘of’,
‘colitas’,
‘rising’,
‘up’,
‘through’,
‘the’,
‘air’,
‘Up’]

Break

break语句将完全打断循环

cleaned_list = []
for word in text.split(' '): if word == 'desert':print('I found the word I was looking for')breakcleaned_list.append(word)
cleaned_list

I found the word I was looking for
[‘On’, ‘a’, ‘dark’]

While循环

如果希望循环在某个时刻结束,我们最终必须使条件为False

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

0
1
2
3
4
5

while True条件使得除非遇到break语句,否则不可能退出循环

如果您陷入无限循环,请使用计算机上的ctrl + c来强制终止

break语句
使用break可以完全退出循环

count = 0
while count <= 5:if count == 2:breakcount += 1print count

1
2

Python零基础入门(一)——Python基础关键字和语法[学习笔记]相关推荐

  1. 关于python编程——从入门到实践一书的详细学习笔记(1)

    一.起步 1.1 .安装 强烈建议使用最新的python3系列 下载安装python环境,这个网上的教程太多了,推荐使用我的老师写的保姆级别教程: https://zhuanlan.zhihu.com ...

  2. 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 三 NSArray

    2019独角兽企业重金招聘Python工程师标准>>> 1 int main(int argc, const char * argv[]) { 2 @autoreleasepool ...

  3. 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 五 NSDicionary + NSMutableDictionary...

    2019独角兽企业重金招聘Python工程师标准>>> 1 int main(int argc, const char * argv[]) { 2 @autoreleasepool ...

  4. 《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 二 NSMutableString

    2019独角兽企业重金招聘Python工程师标准>>> NSMutableString可变字符串 1 int main(int argc, const char * argv[]) ...

  5. 【海码学院】web前端基础入门CSS之常见CSS兼容问题学习笔记

    一.兼容性处理要点 1.DOCTYPE 影响 CSS 处理: 2.FF: 设置 padding 后, div 会增加 height 和 width, 但 IE 不会, 故需要用 !important ...

  6. 视频教程-Python零基础入门教程-Python

    Python零基础入门教程 从2012年从事互联网至今有7年软件编程经验,曾任职国内北京互联网公司,中南林业大学授课Python 现任逻辑教育Python课程负责人,精通Python语言,精通人工智能 ...

  7. 什么是python中子类父类_零基础入门:python中子类继承父类的__init__方法实例

    前言: 今天为大家带来的内容是零基础入门:python中子类继承父类的__init__方法实例!具有不错的参考意义,希望在此能够帮助到各位!(喜欢的话记得点赞转发关注不迷路哦) 使用Python写过面 ...

  8. 零基础入门学Python(十二)—— 魔法方法(下)

    零基础入门学Python系列内容的学习目录→\rightarrow→零基础入门学Python系列内容汇总. 魔法方法(下) 1. 构造和析构 2. 算术运算 3. 简单定制 4. 属性访问 5. 描述 ...

  9. 跟艾文学编程《零基础入门学Python》(1)Python 基础入门

    作者: 艾文,计算机硕士学位,企业内训讲师和金牌面试官,现就职BAT一线大厂公司资深算法专家. 邮箱: 1121025745@qq.com 博客:https://wenjie.blog.csdn.ne ...

最新文章

  1. Android Studio不安装opencv manager配置
  2. leetcode-寻找两个正序数组的中位数
  3. 华为主题锁屏壁纸换不掉_快来看看华为与荣耀手机的这16款主题!别一直用系统默认主题啦!...
  4. 【科学计数法模板讲解】1060 Are They Equal (25 分)
  5. OutOfMemoryError:无法创建新的本机线程–神秘化的问题
  6. python画二维数组散点图_2个numpy二维数组的散点图
  7. (完美解决)Tomcat启动提示At least one JAR was scanned for TLDs yet contained no TLDs
  8. 页面平滑过渡全屏切换
  9. MTK 驱动(62)---eMMC RPMB分区介绍
  10. imx6 android6.0.1,mfgtool刷写i.MX6 android6.0版本失败
  11. 只需三步即可将 Python 程序转换成 exe 文件
  12. Windows10 永久激活查询/激活时间查询/激活查询命令/激活码查询
  13. Python 基于霍夫变换寻找正弦曲线
  14. 黑客劫持域名步骤大曝光
  15. h264基本编码参数
  16. Netty - 探究PageCache磁盘高速缓存
  17. 做区块链联盟链开发前期准备
  18. 基于javaweb的医院门诊收费管理系统(java+jsp+jdbc+mysql)
  19. hosts文件为空白或删除情况修复
  20. MSA应用――MSA手册第四版的新亮点

热门文章

  1. shell判断进程使用CPU时间后kill进程
  2. 小例子背后的大道理——Adapter模式详解
  3. Java 7 最快要到 2012 年中发布
  4. PHP与Unicode签名(BOM)
  5. 能力不是仅靠原始积累(一)
  6. 26muduo_net库源码分析(二)
  7. python pandas 教程_Python pandas十分钟教程
  8. 数据结构与算法之-----栈(Stack)
  9. mysql连接自己的ip地址_mysql 连接字符串 远程连接用IP地址 而非只是localhost时
  10. al00华为手机_jmm-al00是什么型号