【Python 基础 2022 最新】练习 1

举报
我是小白呀iamarookie 发表于 2022/06/19 00:53:17 2022/06/19
【摘要】 【Python 基础 2022 最新】练习 1 概述第一题第二题第三题第四题第一题 (答案)第二题 (答案)第三题 (答案)第四题 (答案) 概述 从今天开始, 小白我将带领大家学习一下 ...

概述

从今天开始, 小白我将带领大家学习一下 Python 零基础入门的内容. 本专栏会以讲解 + 练习的模式, 带领大家熟悉 Python 的语法, 应用, 以及代码的基础逻辑.

在这里插入图片描述

第一题

在这里插入图片描述

def check_types():
    ## Check the data types

    # a. 45 
    # example solution
    print(type(45))
    # the value 45 is classified as an integer in python

    # b. 27.27  

    # the value 27.27 is classified as a float in python

    # c. True  

    # the value True is classified as a boolean in python

    # d. [3, 4, 5, 6]  

    # the value [3, 4, 5, 6] is classified as a list in python

    # e. 'hello'  

    # the value 'hello' is classified as a string in python

check_types()

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

预期输出:

<class 'int'>
<class 'float'>
<class 'bool'>
<class 'list'>
<class 'str'>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

第二题

在这里插入图片描述

def basic_opp(x,y):
    # a. exponentiation (power 2)  
    print(x**2)

    # b. multiplication  
    print(x*y)

    # c. division (y divided by x)
    print(round(y/x,2)) # this rounds the resulting value to two decimal places

    # d. integer division (y divided by x) 
    print(y//x)

    # e. the modulus operator (y modulo x)
    print(y%x)

# set some sample values
a=6
b=8
# run the program
basic_opp(a,b)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

预期输出:

36
48
1.33
1
2

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

第三题

在这里插入图片描述

def string_opp(y):
    # a. In one line, print the first and last characters in the string.


    # b. Print the 6th character in the string.


    # c. Slice off first 5 and last 18 char. result: "listen to your heart"


    # d. Find first instance of letter 'n' in string y.


    # e. Search for '!' in string y. 


    # f. Split the string on each space.


    # g. Combine elements of list z to generate the full sentence as in string y.



# define the string
s="just listen to your heart, that's what I do"

# run the program
string_opp(s)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

预期输出:

j o
l
listen to your heart
10
-1
['just', 'listen', 'to', 'your', 'heart,', "that's", 'what', 'I', 'do']
just listen to your heart, that's what I do

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

第四题

在这里插入图片描述

def boolean_opp(a,b):
    # a.
 

    # b. 


    # c.


    # d. 
 

# define some sample data
n1=7
n2=9

# run the program
boolean_opp(n1,n2)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

预期结果:

True
False
False
True

  
 
  • 1
  • 2
  • 3
  • 4

第一题 (答案)

def basic_opp(x,y):
    # a. exponentiation (power 2)
    print(x**2)

    # b. multiplication
    print(x*y)

    # c. division (y divided by x)
    print(round(y/x,2)) # this rounds the resulting value to two decimal places

    # d. integer division (y divided by x)
    print(y//x)

    # e. the modulus operator (y modulo x)
    print(y%x)

# set some sample values
a=6
b=8
# run the program
basic_opp(a,b)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

第二题 (答案)

def basic_opp(x,y):
    # a. exponentiation (power 2)
    print(x**2)

    # b. multiplication
    print(x*y)

    # c. division (y divided by x)
    print(round(y/x,2)) # this rounds the resulting value to two decimal places

    # d. integer division (y divided by x)
    print(y//x)

    # e. the modulus operator (y modulo x)
    print(y%x)

# set some sample values
a=6
b=8
# run the program
basic_opp(a,b)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

第三题 (答案)

def string_opp(y):
    # a. In one line, print the first and last characters in the string.
    print(y[0],y[-1])

    # b. Print the 6th character in the string.
    print(y[5])

    # c. Slice off first 5 and last 18 char. result: "listen to your heart"
    print(y[5:-18])

    # d. Find first instance of letter 'n' in string y.
    print(y.find('n'))

    # e. Search for '!' in string y. 
    print(y.find('!'))

    # f. Split the string on each space.
    print(y.split(' '))

    # g. Combine elements of list z to generate the full sentence as in string y.
    z=['just', 'listen', 'to', 'your', 'heart,', "that's", 'what', 'I', 'do']
    print(' '.join(z))

# define the string
s="just listen to your heart, that's what I do"

# run the program
string_opp(s)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

第四题 (答案)

def boolean_opp(a,b):
    # a.
    print(a>4 or b<4)

    # b.
    print(not(a>4 or b>4))

    # c.
    print((a>4 and b<4) or (a<4 and b>4))

    # d.
    print((a>4 or b<4) and (a<4 or b>4))

# define some sample data
n1=7
n2=9

# run the program
boolean_opp(n1,n2)

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

文章来源: iamarookie.blog.csdn.net,作者:我是小白呀,版权归原作者所有,如需转载,请联系作者。

原文链接:iamarookie.blog.csdn.net/article/details/125327635

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。