python基础2022最新练习1(代码片段)

我是小白呀 我是小白呀     2022-12-04     621

关键词:

【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()

预期输出:

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

第二题

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)

预期输出:

36
48
1.33
1
2

第三题

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)

预期输出:

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

第四题

def boolean_opp(a,b):
    # a.
 

    # b. 


    # c.


    # d. 
 

# define some sample data
n1=7
n2=9

# run the program
boolean_opp(n1,n2)

预期结果:

True
False
False
True

第一题 (答案)

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  
    print(type(27.27))
    # the value 27.27 is classified as a float in python

    # c. True  
    print(type(True))
    # the value True is classified as a boolean in python

    # d. [3, 4, 5, 6]  
    print(type([3, 4, 5, 6]))
    # the value [3, 4, 5, 6] is classified as a list in python

    # e. 'hello'  
    print(type('hello'))
    # the value 'hello' is classified as a string in python

check_types()

第二题 (答案)

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)

第三题 (答案)

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)

第四题 (答案)

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)

python基础2022最新练习1(代码片段)

【Python基础2022最新】练习1概述第一题第二题第三题第四题第一题(答案)第二题(答案)第三题(答案)第四题(答案)概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python... 查看详情

python基础2022最新练习2(代码片段)

【Python基础2022最新】练习2概述第五题第六题第七题第八题第五题(答案)第六题(答案)第七题(答案)第八题(答案)概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python... 查看详情

python基础2022最新练习2(代码片段)

【Python基础2022最新】练习2概述第五题第六题第七题第八题第五题(答案)第六题(答案)第七题(答案)第八题(答案)概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python... 查看详情

python基础2022最新练习2(代码片段)

【Python基础2022最新】练习2概述第五题第六题第七题第八题第五题(答案)第六题(答案)第七题(答案)第八题(答案)概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python... 查看详情

python基础2022最新第五课函数(代码片段)

【Python基础2022最新】第五课函数概述函数无参函数含参函数参数形参实参变量局部变量全局变量概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python的语法,应用,... 查看详情

python基础2022最新第五课函数(代码片段)

【Python基础2022最新】第五课函数概述函数无参函数含参函数参数形参实参变量局部变量全局变量概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python的语法,应用,... 查看详情

python基础2022最新第五课函数(代码片段)

【Python基础2022最新】第五课函数概述函数无参函数含参函数参数形参实参变量局部变量全局变量概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python的语法,应用,... 查看详情

python基础2022最新第六课numpy(代码片段)

【Python基础2022最新】第六课Numpy概述NumpyNumpy安装Anaconda导包ndarraynp.array创建np.zeros创建np.ones创建常用函数reshapeflatten概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟... 查看详情

python基础2022最新第六课numpy(代码片段)

【Python基础2022最新】第六课Numpy概述NumpyNumpy安装Anaconda导包ndarraynp.array创建np.zeros创建np.ones创建常用函数reshapeflatten概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟... 查看详情

python基础2022最新第四课基础语法(代码片段)

【Python基础2022最新】第四课基础语法概述循环for循环while循环判断语句if判断三元表达式breakcontinuepass概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python的语法,... 查看详情

python基础2022最新第四课基础语法(代码片段)

【Python基础2022最新】第四课基础语法概述循环for循环while循环判断语句if判断三元表达式breakcontinuepass概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python的语法,... 查看详情

python基础2022最新第三课列表&字典(代码片段)

【Python基础2022最新】第三课列表&字典概述列表创建列表列表增删改查索引增删改查其他操作切片操作合并列表查找元素常见错误字典创建字典查看值字典操作访问字典的值添加键值对修改字典键重复概述从今天开始,小白我... 查看详情

python基础2022最新第一课安装&环境配置(代码片段)

【Python基础2022最新】第一课安装&环境配置概述自我介绍安装环境配置Python的历史Python的前景概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟悉Python的语法,应用,... 查看详情

python基础2022最新第一课安装&环境配置(代码片段)

【Python基础2022最新】第一课安装&环境配置概述自我介绍Python的历史Python的前景安装环境配置PyCharm安装第一个程序概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟... 查看详情

python基础2022最新第二课变量&数据类型(代码片段)

【Python基础2022最新】第二课变量&数据类型概述变量创建变量背后的逻辑常见错误数据类型数字型类型非数字型数据类型转换浮点转整数整数转布尔常见错误概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.... 查看详情

python基础2022最新第二课变量&数据类型(代码片段)

【Python基础2022最新】第二课变量&数据类型概述变量创建变量背后的逻辑常见错误数据类型数字型类型非数字型数据类型转换浮点转整数整数转布尔常见错误概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.... 查看详情

2022年最新python大数据之python基础(代码片段)

...inue9、for循环嵌套1、循环介绍有条件的重复做相似的事情Python中循环分为while和for2、while循环的使用格式:while条件:循环体while循环的三个必要元素whil 查看详情

python基础2022最新第六课numpy(代码片段)

【Python基础2022最新】第六课Numpy概述NumpyNumpy安装Anaconda导包ndarraynp.array创建np.zeros创建np.ones创建常用函数reshapeflatten概述从今天开始,小白我将带领大家学习一下Python零基础入门的内容.本专栏会以讲解+练习的模式,带领大家熟... 查看详情