程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

【Python 基礎 2022 最新】練習 1

編輯:Python

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved