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类型()

相关推荐
源码哥_博纳软云12 分钟前
JAVA同城服务场馆门店预约系统支持H5小程序APP源码
java·开发语言·微信小程序·小程序·微信公众平台
学会沉淀。19 分钟前
Docker学习
java·开发语言·学习
如若12320 分钟前
对文件内的文件名生成目录,方便查阅
java·前端·python
西猫雷婶1 小时前
python学opencv|读取图像(二十一)使用cv2.circle()绘制圆形进阶
开发语言·python·opencv
kiiila1 小时前
【Qt】对象树(生命周期管理)和字符集(cout打印乱码问题)
开发语言·qt
小_太_阳1 小时前
Scala_【2】变量和数据类型
开发语言·后端·scala·intellij-idea
直裾1 小时前
scala借阅图书保存记录(三)
开发语言·后端·scala
老刘莱国瑞1 小时前
STM32 与 AS608 指纹模块的调试与应用
python·物联网·阿里云
唐 城2 小时前
curl 放弃对 Hyper Rust HTTP 后端的支持
开发语言·http·rust
一只敲代码的猪2 小时前
Llama 3 模型系列解析(一)
大数据·python·llama