Python 入门教程 6 ---- PygLatin

简介:  第一节      1 练习:使用Python来输出这句话"Welcome to the English to Pig Latin translator!" print "Welcome to the English to Pig L...


 第一节

     1 练习:使用Python来输出这句话"Welcome to the English to Pig Latin translator!"

print "Welcome to the English to Pig Latin translator!"


 第二节

     1 介绍了Python的输入,Python里面我们可以通过raw_input来实现出入

     2 比如我们使用name = raw_ijnput("what's your name") , 那么这里将会在what's your name提示我们输入,并把结果保存到name里面

     3 练习:使用original变量来接受任何的输入

print "Welcome to the English to Pig Latin translator!"
original = raw_input("welcome to the Python:")


 第三节

    1 介绍了我们在输入的时候经常出现输入空字符串的情况,因此我们需要去检查是否是空字符串

    2 练习:在上一节的输入的值进行判断,如果不是空字符串那么打印这个值,否则直接输出"empty"

print "Welcome to the English to Pig Latin translator!"
original = raw_input("welcome to the Python:")
if len(original) > 0:
    print original
else:
    print "empty"


 第四节

   1 介绍了怎样判断一个字符串是数字的方法,我们可以通过isalpha()来判断

   2 比如有一个变量为x = "123",那么x.isalpha()是True

   3 练习:通过变量original的输入值来判断是否是一个数字串

print "Welcome to the English to Pig Latin translator!"
original = raw_input("welcome to the Python:")

if original.isalpha():
    print "True"
else:
    print "False"


 第五节

    1 练习:利用多次的输入来判断是否是数字串和非空字符串

print "Welcome to the English to Pig Latin translator!"
original = raw_input("welcome to the Python:")
if original.isalpha():
    print "True"
else:
    print "False"
    
original = raw_input("welcome to the Python:")
if len(y) == 0:
    print "empty"
else:
    print "no empty"


 第六节

    1 回顾了一下之前的String的lower()函数

    2 练习

       1 设置变量word等于original.lower()

       2 设置变量first等于word的第一个字符

pyg = 'ay'

original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
if len(original) > 0 and original.isalpha():
    print original
else:
    print 'empty'


 第六节

    1 介绍了if语句里面还可以嵌套语句

    2 练习:判断上一节里面的first字符是否是元音字符是的话输出"vowel",否则输出"consonant"

pyg = 'ay'

original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
if len(original) > 0 and original.isalpha():
    if first == 'a' or first == 'i' or first == 'o' or first == 'u' or first == 'e':
        print "vowel"
    else:
        print "consonant"
else:
    print 'empty'


 第七节

     1 利用String的+操作,产生一个新的变量new_word等于word+pyg

     2 练习:把print "vowel"替换成print new_word

pyg = 'ay'
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
new_word = word+pyg

if len(original) > 0 and original.isalpha():
    if first == 'a' or first == 'i' or first == 'o' or first == 'u' or first == 'e':
        print new_word
    else:
        print "consonant"
else:
    print 'empty'


 第八节

    1 介绍了String中得到子串的方法,比如我们有一个字符串s = "foo",现在s[0:2]就是"fo"

    2 如果结束是末尾,那么可以直接写成这样s[i:],省略第二个数

    2 练习:在嵌套的if语句里面设置new_word的值为word从第1位到最后一位+变量pyg

pyg = 'ay'
original = raw_input('Enter a word:')
word = original.lower()
first = word[0]
new_word = word+pyg

if len(original) > 0 and original.isalpha():
    if first == 'a' or first == 'i' or first == 'o' or first == 'u' or first == 'e':
        new_word = word[1:]+pyg
        print new_word
    else:
        print "consonant"
else:
    print 'empty'


目录
相关文章
|
6月前
|
Python Windows
Python入门教程:从基础到实践
Python入门教程:从基础到实践
78 0
|
6月前
|
Ubuntu Shell 数据库
Python Qt5 入门教程
Python Qt5 入门教程
111 1
|
7月前
|
机器学习/深度学习 数据采集 数据挖掘
Python 数据分析入门教程:Numpy、Pandas、Matplotlib和Scikit-Learn详解
Python 数据分析入门教程:Numpy、Pandas、Matplotlib和Scikit-Learn详解
142 0
|
1月前
|
JSON C语言 C++
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
【Python 基础教程 26】Python3标准库全面入门教程:一步步带你深入理解与应用
63 1
|
1月前
|
算法 程序员 C++
【Python 基础教程 运算符06】Python3运算符超详细解析:全面入门教程,初学者必读
【Python 基础教程 运算符06】Python3运算符超详细解析:全面入门教程,初学者必读
92 2
|
1月前
|
算法 程序员 C++
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
108 1
|
1月前
|
存储 程序员 C++
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
【Python 基础教程 03 类型转换】从隐式到显式:全面理解Python数据类型转换的超详细初学者入门教程
50 0
|
3月前
|
机器学习/深度学习 测试技术 开发者
最新PyCharm下载安装以及Python环境搭建教程(含Python入门教程)
最新PyCharm下载安装以及Python环境搭建教程(含Python入门教程)
188 1
|
6月前
|
监控 应用服务中间件 nginx
软件开发入门教程网之Python uWSGI 安装配置
软件开发入门教程网之Python uWSGI 安装配置
27 0
|
6月前
|
SQL 关系型数据库 MySQL
软件开发入门教程网之Python MySQL - mysql-connector 驱动 2
软件开发入门教程网之Python MySQL - mysql-connector 驱动
35 0