zoukankan      html  css  js  c++  java
  • python3 _笨方法学Python_日记_DAY1

    第四版 资源https://pan.baidu.com/s/1bqvf0Ov

    从零开始!每个细节都要过

    书中是根据python2.5写的

    我用的是py3,顺便熟悉一下两者的区别

    • Day1 
    • “习题一:第一个程序”

    print("hello world!")
    print("Hello again")
    print("I like typing this.")
    print("This is fun.")
    print('Yay!Printing.')
    print("I'd much rather you 'not'.")
    print('I "said" do not touch this.')

    结果:

    hello world!
    Hello again
    I like typing this.
    This is fun.
    Yay!Printing.
    I'd much rather you 'not'.

    I "said" do not touch this.

    尝试:玩一玩单引号和双引号

    #单引号双引号尝试
    print('1')
    print("1")

    1

    1

    似乎是一样的,那么

    print('1")

      File "E:/py/learn python the hard way/Day1/01.py", line 12
        print('1")
                 ^

    SyntaxError: EOL while scanning string literal

    字符串,引号没有成对出现,报错,看来并不完全一样

    再试试

    print("I'd love to")
    print('I'd love to')

    第二个报错

        print('I'd love to')
                 ^

    SyntaxError: invalid syntax

    所以双引号可以避免这种句子中需要单引号的情况

    • 习题  3:  数字和数学计算

    print("I'll now count my chickens:")
    print("Hens",25+30/6)
    print("Roosters",100-25*3%4)
    print("Now I will count the eggs:")
    print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
    print("It is true that 3+2<5-7?")
    print(3+2<5-7)
    print("what is 3+2?",3+2)
    print("what is 5-7?",5-7)
    print("Oh that's why it is false.")
    print("How about some more.")
    print("Is it greater?",5-2)
    print("Is it greater or equal?",5>=-2)
    print("Is it less or equal?",5<=-2)

    Hens 30.0
    Roosters 97
    Now I will count the eggs:
    6.75
    It is true that 3+2<5-7?
    False
    what is 3+2? 5
    what is 5-7? -2
    Oh that's why it is false.
    How about some more.
    Is it greater? 3
    Is it greater or equal? True
    Is it less or equal? False

    结果与书上有出入的有两行,应该是PY2和PY3的区别

    print("Hens",25+30/6)
    30.0
    print(3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6)
    6.75

    书上结果为30和7

    主要在于除法的运算上,

    py2:一个整数(无小数部分的数)被另外一个整数除,计算结果的小数部分被截除了,只留下了整数部分

    所以1/4=0 所以第二条结果为7

    在Python3.0中变成真除法(无论任何类型都会保持小数部分,即使整除也会表示为浮点数形式)

    • 习题4:变量(variable)和命名

    cars=100
    space_in_a_car=4.0
    drivers=30
    passengers=90
    cars_not_driven=cars-drivers
    cars_driven=drivers
    carpool_capacity=cars_driven*space_in_a_car
    average_passengers_per_car=passengers/cars_driven

    print("there are",cars,"cars available")
    print("there are only",drivers,"drivers available")
    print("there will be",cars_not_driven,"empty cars today")
    print("we can transport",carpool_capacity,"people today")
    print("we have",passengers,"to carpool today")
    print("we need to put about",average_passengers_per_car,"in each car")

    结果

    there are 100 cars available
    there are only 30 drivers available
    there will be 70 empty cars today
    we can transport 120.0 people today
    we have 90 to carpool today
    we need to put about 3.0 in each car

    加分题:

    Traceback (most recent call last):
    File "ex4.py", line 8, in <module>
    average_passengers_per_car = car_pool_capacity /
    passenger
    NameError: name 'car_pool_capacity' is not defined

    #报错原因:car_pool_capacity 没有被定义

    • 习题5:更多的变量和打印

    my_name='mrfri'
    my_age=23
    my_height=178#cm
    my_weight=60#kg
    my_eyes='Brown'
    my_teeth='White'
    my_hair='Black'
    print("Let's talk about %s."%my_name)
    print("He is %d cms high."%my_height)
    print("He is %d kgs heavy."%my_weight)
    print("He's got %s eyes and %s hair.His teeth are %s."%(my_eyes,my_hair,my_teeth))
    print("If I add %d,%d and %d,then I get: %d."%(my_age,my_weight,my_height,my_age+my_height+my_weight))

    Let's talk about mrfri.
    He is 178 cms high.
    He is 60 kgs heavy.
    He's got Brown eyes and Black hair.His teeth are White.
    If I add 23,60 and 178,then I get: 261.

    格式化字符:

    %% 百分号标记 #就是输出一个%
    %c 字符及其ASCII码
    %s 字符串
    %d 有符号整数(十进制)
    %u 无符号整数(十进制)
    %o 无符号整数(八进制)
    %x 无符号整数(十六进制)
    %X 无符号整数(十六进制大写字符)
    %e 浮点数字(科学计数法)
    %E 浮点数字(科学计数法,用E代替e)
    %f 浮点数字(用小数点符号)
    %g 浮点数字(根据值的大小采用%e或%f)
    %G 浮点数字(类似于%g)
    %p 指针(用十六进制打印值的内存地址)
    %n 存储输出字符的数量放进参数列表的下一个变量中

    %r 照原样打印出来,例如字符串会打印出引号

    测试:

    first='a'
    second=1-17
    third=0.0008320320156

    print("first is %c"%first)
    print("有符号整数%d十进制"%second)
    print("无符号整数%u十进制"%second)
    print("无符号整数%o八进制"%second)
    print("无符号整数%x十六进制"%second)
    print("浮点数字%f小数点"%third)
    print("浮点数字%e科学计数法"%third)
    print("自动判断用e还是f,%g"%third)

    有符号整数-16十进制
    无符号整数-16十进制
    无符号整数-20八进制
    无符号整数-10十六进制
    浮点数字0.000832小数点
    浮点数字8.320320e-04科学计数法
    自动判断用e还是f,0.000832032

    可见,似乎在python3中不存在有无符号的说法,统一打成有符号的?

    今天就到这吧,从实例入手感觉还不错,比较好玩,比光看书有意思多了。

    坚持!

  • 相关阅读:
    关于ShareSDK接入的各种问题,以及解决方案
    Cocos2d-x使用iOS游戏内付费IAP(C++篇)
    数论——终结素数判定
    poj 2528 线段树+离散化
    STL 优先队列
    poj 2777 线段树+延迟更新
    RMQ
    codeforces 拼手速题2
    codeforces 拼手速题1
    子矩阵 思维吧
  • 原文地址:https://www.cnblogs.com/mrfri/p/8449096.html
Copyright © 2011-2022 走看看