Learn the basics of Python 3-Code Challenges:Loops

这篇具有很好参考价值的文章主要介绍了Learn the basics of Python 3-Code Challenges:Loops。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

   1.Coding question 1 Divisible By Ten

Create a function named divisible_by_ten() that takes a list of numbers named nums as a parameter.
Return the count of how many numbers in the list are divisible by 10.

def divisible_by_ten(nums):
  count = 0
  for number in nums:
    if (number % 10 == 0):
      count += 1
  return count

print(divisible_by_ten([20, 25, 30, 35, 40]))

 2.Coding question 2 Greetings

Create a function named add_greetings() which takes a list of strings named names as a parameter.In the function, create an empty list that will contain each greeting. Add the string 'Hello, ' in front of each name in names and append the greeting to the list.Return the new list containing the greetings.

def add_greetings(names):
  new_list = []
  for name in names:
    new_list.append("Hello, " + name)
  return new_list

print(add_greetings(["Owen", "Max", "Sophie"]))

 3.Coding question 3

Write a function called delete_starting_evens() that has a parameter named my_list.
The function should remove elements from the front of my_list until the front of the list is not even. The function should then return my_list.
For example if my_list started as [4, 8, 10, 11, 12, 15], then delete_starting_evens(my_list) should return [11, 12, 15].
Make sure your function works even if every element in the list is even!

#Write your function here  (Can you find the problem?)
def delete_starting_evens(my_list):
  for num in my_list:
    if num % 2 == 0:
      my_list.remove(num)
    else:
      break
  return my_list

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
# =>[8, 11, 15]
print(delete_starting_evens([4, 8, 10]))
# =>[8]

#This is the way we solved it:
def delete_starting_evens(my_list):
  while (len(my_list) > 0 and my_list[0] % 2 == 0):
    my_list = my_list[1:]
  return my_list

print(delete_starting_evens([4, 8, 10, 11, 12, 15]))
# =>[11, 12,15]
print(delete_starting_evens([4, 8, 10]))
# =>[]

4.Coding question 4 Odd Indices

Create a function named odd_indices() that has one parameter named my_list.
The function should create a new empty list and add every element from my_list that has an odd index. The function should then return this new list.
For example, odd_indices([4, 3, 7, 10, 11, -2]) should return the list [3, 10, -2].

def odd_indices(my_list):
  new_my_list = []
  for i in range(len(my_list)):
    if i % 2 == 1:
      new_my_list.append(my_list[i])
  return  new_my_list

print(odd_indices([4, 3, 7, 10, 11, -2]))
# =>[3, 10, -2]

#Here is this solution:
def odd_indices(my_list):
  new_list = []
  for index in range(1, len(my_list), 2):
    new_list.append(my_list[index])
  return new_list

print(odd_indices([4, 3, 7, 10, 11, -2]))
# =>[3, 10, -2]

 5.Coding question 5 Exponents

Create a function named exponents() that takes two lists as parameters named bases and powers. Return a new list containing every number in bases raised to every number in powers.
For example, consider the following code.exponents([2, 3, 4], [1, 2, 3])

def exponents(bases, powers):
  my_list = []
  for num1 in bases:
    for num2 in powers:
      my_list.append(num1**num2)
  return my_list


print(exponents([2, 3, 4], [1, 2, 3]))
# =>[2, 4, 8, 3, 9, 27, 4, 16, 64]

#Here is how we solved this one:
def exponents(bases, powers):
  new_list = []
  for base in bases:
    for power in powers:
      new_list.append(base ** power)
  return new_list

print(exponents([2, 3, 4], [1, 2, 3]))
# =>[2, 4, 8, 3, 9, 27, 4, 16, 64]

Learn the basics of Python 3-Chapter 4:Loops

Learn the basics of Python 3-Code Challenges:Loops(Advanced)文章来源地址https://www.toymoban.com/news/detail-824078.html

到了这里,关于Learn the basics of Python 3-Code Challenges:Loops的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Blockchain for Internet of Energy management: Review, solutions, and challenges

    本文是《Blockchain for Internet of Energy management: Review, solutions, and challenges》的中文翻译。 继智能电网之后,通过整合不同形式的能源,能源互联网(IoE)已成为能源领域的一项流行技术。IoE利用互联网收集、组织、优化和管理来自不同边缘设备的网络能源信息,以开发分布式智

    2024年02月02日
    浏览(43)
  • Qt中实现界面回放的艺术:从理论到代码“ (“The Art of Implementing UI Playback in Qt: From Theory to Code

    在这个快速发展的数字化时代,界面回放技术(UI Playback Technology)在软件开发中扮演了至关重要的角色。这项技术不仅提高了软件测试的效率,还为用户交互设计提供了深刻的洞察。通过回放用户与界面的交互过程,开发者能够更好地理解用户行为,优化用户体验。正如计算

    2024年01月20日
    浏览(57)
  • Code Block & Basic Block

    In a programming language, a code block typically starts with certain syntactical constructs such as loops, conditionals, or function definitions. When a compiler walks the Abstract Syntax Tree (AST), it uses this syntax information to recognize the beginning of a new code block. Here are some examples: Loop Statements : for , while , or do-while loops usual

    2024年02月11日
    浏览(36)
  • 【论文笔记】A Survey of Large Language Models in Medicine - Progress, Application, and Challenges

    将LLMs应用于医学,以协助医生和病人护理,成为人工智能和临床医学领域的一个有前景的研究方向。为此, 本综述提供了医学中LLMs当前进展、应用和面临挑战的全面概述 。 具体来说,旨在回答以下问题: 1)什么是LLMs,如何构建医学LLMs? 2)医学LLMs的下游表现如何? 3)

    2024年02月03日
    浏览(44)
  • 论文笔记:A Comprehensive Review of Indoor/Outdoor Localization Solutions in IoT era: Research Challenges

    Computer Networks 2022 本地化或定位 使用某些固定节点和移动计算设备来识别可移动/固定设备(智能手机、无人机、手表、信标和车辆)的位置 户外定位【GPS、北斗。。。】 在城市地区和室内环境中存在一些局限性 室内环境更复杂,周围有许多物体 信号干扰 建筑物内的反射高

    2024年02月03日
    浏览(41)
  • Pytorch Tutorial【Chapter 1. Basic operation of tensor】

    本节介绍有关张量 Tensor 的基本操作 Tensor 相当于 numpy 中的 ndarrays 创建空 Tensor 和全零 Tensor , torch.zeros(d0,d1) 类似于 numpy.zeros(size) , torch.empty(d0,d1) 类似于 numpy.empty(size) 创建随机 Tensor , torch.rand(d0,d1) 相当于 numpy.random.rand(d0,d1) , rand 表示随机变量服从uniform distribution, tor

    2024年02月13日
    浏览(38)
  • Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding

    win10 环境下执行 python -v 命令后报错,报错信息中含有如题字段 查询网络后得知原因是安装不同版本python的结果,解决方法如下: 通过windows系统卸载现有python软件; 删除环境变量 path 中现有python相关环境变量; 重新安装python,记得勾选配置环境变量选项,这样,系统环境变

    2023年04月08日
    浏览(43)
  • 【PyTorch】6.Learn about the optimization loop 了解优化循环

    现在我们有了模型和数据,是时候通过优化数据上的参数来训练、验证和测试我们的模型了。训练模型是一个迭代过程;在每次迭代中,模型都会对输出进行猜测,计算其猜测中的误差( 损失 ),收集相对于其参数的导数的误差(如我们在上一节中看到的),并使用梯度下

    2024年01月23日
    浏览(37)
  • Learn the architecture - Understanding Armv9-A trace

    快速链接: . 👉👉👉 个人博客笔记导读目录(全部) 👈👈👈 付费专栏-付费课程 【购买须知】: 【精选】ARMv8/ARMv9架构入门到精通-[目录] 👈👈👈 — 适合小白入门 【目录】ARMv8/ARMv9架构高级进阶-[目录]👈👈👈 — 高级进阶、小白勿买 【加群】ARM/TEE/ATF/SOC/芯片/安全-学习交

    2024年02月04日
    浏览(49)
  • Learn the architecture - Debugger usage on Armv8- A

    快速链接: . 👉👉👉 个人博客笔记导读目录(全部) 👈👈👈 付费专栏-付费课程 【购买须知】: 【精选】ARMv8/ARMv9架构入门到精通-[目录] 👈👈👈 — 适合小白入门 【目录】ARMv8/ARMv9架构高级进阶-[目录]👈👈👈 — 高级进阶、小白勿买 【加群】ARM/TEE/ATF/SOC/芯片/安全-学习交

    2024年02月05日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包