python 第一天

循环打印

while循环

复制代码
languages = ["Regional Assembly Language","Autocode","FORTRAN","IPL (LISP的先驱)","FLOW-MATIC (COBOL的先驱)","COMTRAN (COBOL的先驱)","LISP","ALGOL 58","FACT (COBOL的先驱)","COBOL","APL","Simula","SNOBOL","CPL (C的先驱)","BASIC","PL/I","BCPL (C的先驱)","Logo","Pascal","Forth","C语言","Smalltalk","Prolog","ML","Scheme","SQL","Ada","C++","Common Lisp","MATLAB","Eiffel","Objective-C","Erlang","Perl","Tcl","FL (Backus)","Haskell","Python","Visual Basic","HTML","Ruby","Lua","CLOS (part of ANSI Common Lisp)","Java","Delphi (Object Pascal)","JavaScript","PHP","REBOL","D","C#","Visual Basic .NET","F#","Scala","Factor","Windows PowerShell","Rust","Clojure","Go"]
years = [1951, 1952, 1954, 1954, 1955, 1957, 1958, 1958, 1959, 1959, 1962, 1962, 1962, 1963, 1964, 1964, 1967 ,1968 ,1970 ,1970 ,1972 ,1972 ,1972 ,1973 ,1975 ,1978 ,1980 ,1983 ,1984 ,1984 ,1985 ,1986 ,1986 ,1987 ,1988 ,1989 ,1990 ,1991 ,1991 ,1991 ,1993 ,1993 ,1994 ,1995 ,1995 ,1995 ,1995 ,1997 ,1999 ,2001 ,2001 ,2002 ,2003 ,2003 ,2006 ,2006 ,2007 ,2009]
i = 0
while i < len(years):
    language = languages[i]
    year = years[i]
    print(language, ':', year)
    i += 1

for循环

复制代码
languages = ["Regional Assembly Language","Autocode","FORTRAN","IPL (LISP的先驱)","FLOW-MATIC (COBOL的先驱)","COMTRAN (COBOL的先驱)","LISP","ALGOL 58","FACT (COBOL的先驱)","COBOL","APL","Simula","SNOBOL","CPL (C的先驱)","BASIC","PL/I","BCPL (C的先驱)","Logo","Pascal","Forth","C语言","Smalltalk","Prolog","ML","Scheme","SQL","Ada","C++","Common Lisp","MATLAB","Eiffel","Objective-C","Erlang","Perl","Tcl","FL (Backus)","Haskell","Python","Visual Basic","HTML","Ruby","Lua","CLOS (part of ANSI Common Lisp)","Java","Delphi (Object Pascal)","JavaScript","PHP","REBOL","D","C#","Visual Basic .NET","F#","Scala","Factor","Windows PowerShell","Rust","Clojure","Go"]
years = [1951, 1952, 1954, 1954, 1955, 1957, 1958, 1958, 1959, 1959, 1962, 1962, 1962, 1963, 1964, 1964, 1967 ,1968 ,1970 ,1970 ,1972 ,1972 ,1972 ,1973 ,1975 ,1978 ,1980 ,1983 ,1984 ,1984 ,1985 ,1986 ,1986 ,1987 ,1988 ,1989 ,1990 ,1991 ,1991 ,1991 ,1993 ,1993 ,1994 ,1995 ,1995 ,1995 ,1995 ,1997 ,1999 ,2001 ,2001 ,2002 ,2003 ,2003 ,2006 ,2006 ,2007 ,2009]
i = 0
for i in range(0, len(languages)):
    language = languages[i]
    year = years[i]
    print(language, ':',year)
    i += 1

陌生语法:把for循环套在print函数里

复制代码
languages = ["Regional Assembly Language","Autocode","FORTRAN","IPL (LISP的先驱)","FLOW-MATIC (COBOL的先驱)","COMTRAN (COBOL的先驱)","LISP","ALGOL 58","FACT (COBOL的先驱)","COBOL","APL","Simula","SNOBOL","CPL (C的先驱)","BASIC","PL/I","BCPL (C的先驱)","Logo","Pascal","Forth","C语言","Smalltalk","Prolog","ML","Scheme","SQL","Ada","C++","Common Lisp","MATLAB","Eiffel","Objective-C","Erlang","Perl","Tcl","FL (Backus)","Haskell","Python","Visual Basic","HTML","Ruby","Lua","CLOS (part of ANSI Common Lisp)","Java","Delphi (Object Pascal)","JavaScript","PHP","REBOL","D","C#","Visual Basic .NET","F#","Scala","Factor","Windows PowerShell","Rust","Clojure","Go"]
years = [1951, 1952, 1954, 1954, 1955, 1957, 1958, 1958, 1959, 1959, 1962, 1962, 1962, 1963, 1964, 1964, 1967 ,1968 ,1970 ,1970 ,1972 ,1972 ,1972 ,1973 ,1975 ,1978 ,1980 ,1983 ,1984 ,1984 ,1985 ,1986 ,1986 ,1987 ,1988 ,1989 ,1990 ,1991 ,1991 ,1991 ,1993 ,1993 ,1994 ,1995 ,1995 ,1995 ,1995 ,1997 ,1999 ,2001 ,2001 ,2002 ,2003 ,2003 ,2006 ,2006 ,2007 ,2009]

[print(languages[i], ':', years[i]) for i in range(0, len(languages))]

问题:编写一个程序,它将找到所有这些数字,可被7整除,但不是5的倍数,2000年至3200年(包括在内)。得到的数字应按逗号分隔的顺序打印在一行上。

提示:考虑使用range(#begin, #end)方法

复制代码
l = []
for i in range(2000 , 3201):
    if (i%7==0) and (i%5!=0):
        l.append(str(i))
        
print(','.join(l))

join的使用:将容器之中的各个元素之间用指定符号连接。

问题:编写一个可以计算给定数的阶乘的程序。结果应该以逗号分隔的顺序打印在一行上。假设向程序提供以下输入:8

则输出为:40320

提示:在为问题提供输入数据的情况下,应该假设它是控制台输入。

复制代码
def fact(x):
    if x == 0:
        return 1
    return x * fact(x - 1)
x = int(input('请输入一个数字:'))
print(fact(x))

问题:使用给定的整数n,编写一个程序生成一个包含(i, i*i)的字典,该字典包含1到n之间的整数(两者都包含)。然后程序应该打印字典。

假设向程序提供以下输入:8

则输出为:

{1:1,2:4,3:9,4:16,5:25,6:36,,7:49,8:64}

提示:在为问题提供输入数据的情况下,应该假设它是控制台输入。考虑使用dict类型()

相关推荐
StayInLove3 分钟前
G1垃圾回收器日志详解
java·开发语言
无尽的大道11 分钟前
Java字符串深度解析:String的实现、常量池与性能优化
java·开发语言·性能优化
爱吃生蚝的于勒14 分钟前
深入学习指针(5)!!!!!!!!!!!!!!!
c语言·开发语言·数据结构·学习·计算机网络·算法
binishuaio24 分钟前
Java 第11天 (git版本控制器基础用法)
java·开发语言·git
zz.YE25 分钟前
【Java SE】StringBuffer
java·开发语言
就是有点傻30 分钟前
WPF中的依赖属性
开发语言·wpf
洋24038 分钟前
C语言常用标准库函数
c语言·开发语言
进击的六角龙40 分钟前
Python中处理Excel的基本概念(如工作簿、工作表等)
开发语言·python·excel
wrx繁星点点41 分钟前
状态模式(State Pattern)详解
java·开发语言·ui·设计模式·状态模式
NoneCoder1 小时前
Java企业级开发系列(1)
java·开发语言·spring·团队开发·开发