Python新手学习(一):基础和控制流

本文概述了Python编程的入门知识,包括基本语法、数学运算、数据类型、变量与赋值、函数、控制流结构(如条件语句和循环)、模块导入以及简单的猜数字和石头剪刀布游戏示例。
摘要由CSDN通过智能技术生成

       把《Python 编程快速上手》过了一遍。

       Python很类似于shell脚本,不太区分变量类型,但复杂数据结构上考虑的不错,列表、元组、字典,基本上可以涵盖各种数据组织模型

1、python基础
  1)数学操作符 ** % // / * - +  
    //:整除  **:乘方
  2)数据类型 整型 浮点型 字符串
  3)字符串连接和复制
    连接 ‘xxx’+’yyy’
    复制 ‘xxx’ * int
    + * 字符操作符
  4)变量和赋值
    同类型可加,异类型不能加。
    无效的变量名
  5)第一个程序 
    test_101.py 输入名字和年龄,并显示     

# This program says hello and asks for my name.Name
import sys

print('len argv:' + str(len(sys.argv)))
if len(sys.argv) >= 2:
    print("hello world %s" % sys.argv[1])
else:
    print ('Hello , world !!')

print ('你的名字?What is your name?') #ask for their name
myName = input ()
print ('It is good to meet you ,' + myName)
print(len(myName))
print("What is your age?") #ask for their age
myAge = input()
print('You will be '+str(int(myAge) + 1) + ' in a year.')

   6)函数

    注释 #
    print()函数
    input()函数
    变量名及输出
    len()函数
    str()、int()、float()
    int()不能转换浮点的字符串,但能转换浮点数。

2、Python控制流
  1)布尔值 True False  首字母大写
  2)比较操作符
    == != < > <= >=
  3)布尔操作符  and or not
    优先级 先not 次and 后or
  4)混和布尔操作符和比较操作符
  5)控制流的元素 条件语句和代码块
    测试程序 :test_201.py

name  = 'Mary'
sex  = 'man'
password = 'swordfish'
if name == 'Mary':
    print('Hello, Mary')
    if password == 'swordfish':
        print('Access granted.')
    else:
        print('Wrong password.')
elif sex == 'woman':
    print('Error Name!! Girl')
else :
    print('Error name!! Boy')

  6)程序执行 
  7)控制流语句
    测试程序:test_202.py

name = 'Carol'
#name = 'Alice'
age = 30
if name == 'Alice':
    print('Hi, Alice')
elif age < 12:
    print('You are not Alice,kiddo.')
elif age > 2000:
    print('Unlike you, Alice is not an undead,immortl vampire.')
elif age > 100:
    print('You are not Alice, grannie')
else :
    print('You are a adult.')

while age < 35:
    print('Hello world !' + name + ' ' + str(age))
    age = age + 1

print ('My name is ')
for i in range(0,10,2):
    print('Jimmy Five Times (' + str(i) +')')

name = ''
while name != 'your name':
    print('Please type your name:')
    name = input()
    if name == 'exit':
        break
print('Thank you!')

    If语句 关键字 条件 冒号 缩进的代码块
    else语句 关键字 冒号 缩进的代码块
    elif语句 关键字 条件 冒号 缩进的代码块
    while语句 关键字 条件 冒号 缩进的代码块
    break语句
    continue语句
    for循环
    range()函数 开始、停止、步长
  8)导入模块
    测试程序 test_203.py

import random,sys
for i in range(5):
    print(random.randint(1,10))

while True:
    print('Type exit to exit.')
    response = input()
    if response == 'exit':
        sys.exit()
    print('You typed ' + response + '.')

    import 模块名
    sys.exit()
  9)猜数字
    测试程序 test_204.py

#This is a guess the number game.
import random
secretNumber = random.randint(1,20)
print('I am thinking of a number between 1 and 20.')

#Ask the player to guess 6 times.
for guessesTaken in range(1,7):
    print('Take a guess.')
    guess  = int(input())

    if guess < secretNumber:
        print('Your guess is too low')
    elif guess > secretNumber:
        print('Your guess is too high.')
    else:
        break # This condition is the correct guess!

if guess == secretNumber:
    print('Good job! You guesssed my number in ' + str(guessesTaken) + ' guesses!')
else:
    print('Nope. The number I was thinking of was ' + str(secretNumber))

  10)石头剪刀布
    测试程序 test_106.py

import random,sys

print("ROCK, PAPER, SCISSORS")

#These variables keep track of the number of wins,losses ,and ties.
wins = 0
losses = 0
ties = 0

while True:  # The main game loop.
    print('%s Wins, %s Losses, %s Ties' %(wins,losses,ties))
    while True: # The player input loop
        print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit')
        playerMove = input()
        if playerMove == 'q':
            sys.exit()   #Quit the program
        if playerMove == 'r' or playerMove == 'p' or playerMove == 's':
            break  #Break out of the player input loop.
        print('Type on of r,p,s,or q.')

    # Display what the player chose
    if playerMove == 'r':
        print('ROCK versus...')
    elif playerMove == 'p':
        print('PAPER versus...')
    elif playerMove == 's':
        print('SCISSORS versus...')

    # Display what the computer chose:
    randomNumber = random.randint(1,3)
    if randomNumber == 1:
        computerMove = 'r'
        print('ROCK')
    elif randomNumber == 2:
        computerMove = 'p'
        print('PAPER')
    elif randomNumber == 3:
        computerMove = 's'
        print('SCISSORS')
    
    # Display and record the win/loss/tie:
    if playerMove == computerMove:
        print('It is a tie!')
        ties = ties +1
    elif playerMove == 'r' and computerMove == 's':
        print('You win!')
        wins = wins + 1
    elif playerMove == 'p' and computerMove == 'r':
        print ('You win!')
        wins = wins + 1
    elif playerMove == 's' and computerMove == 'p':
        print('You win!')
        wins = wins + 1
    elif playerMove == 'r' and computerMove == 'p':
        print('You lose!')
        losses = losses + 1
    elif playerMove == 'p' and computerMove == 's':
        print('You lose!')
        losses = losses + 1
    elif playerMove == 's' and computerMove =='r':
        print('You lose!')
        losses = losses + 1

  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值