python笔记

1、安装环境

2、print "Hello World!"

Hello World!

3、注释,井号:#

语法错误,加入# -*- coding: utf-8 -* 

学习编程才是更重要的事情。 如果 # 是注解的意思,那么为什么 # -*- coding: utf-8 -*- 能起作用呢? Python 其实还是没把这行当做代码处理,这种用法只是让字符格式被识别的一个取巧的方案,或 者说是一个没办法的办法吧。在编辑器设置里你还能看到一个类似的注解。 为什么 print "Hi # there." 里的 # 没被忽略掉? 这行代码里的 # 处于字符串内部,所以它就是引号结束前的字符串中的一部分,这时它只是一个 普通字符,而不代表注解的意思。 怎样做多行注解? 每行前面放一个 # 就可以了。

4、数字和数学计算

• + plus 加号

• - minus 减号

• / slash 斜杠

• * asterisk 星号

• % percent 百分号

• < less-than 小于号

• > greater-than 大于号

• <= less-than-equal 小于等于号

• >= greater-than-equal 大于等于号 

5、变量和命名

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."

 

$ python ex4.py

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 in each car.

space_in_a_car 中的 _ 是 下划线(underscore) 字符。你要自己学会怎样打出这个字符来。 这个符号在变量里通常被用作假想的空格,用来隔开单词。

= 和 == 有什么不同? = (single-equal) 的作用是将右边的值赋予左边的变量名。`==` (double-equal) 的作用是检查左右 离岸边是否相等。习题 27 中你会学到 == 的用法。 写成 x=100 而非 x = 100 也没关系吧? 是可以这样写,但这种写法不好。操作符两边加上空格会让代码更容易阅读。 print 时词语间的空格有没有办法不让打印出来? 你可以通过这样的方法实现: print "Hey %s there." % "you",后面马上就会讲到。

怎样倒着读代码? 很简单,假如说你的代码有 16 行,你就从第 16 行开始,和我的第 16 行比对,接着比对第 15 行,以此类推,直到全部检查完

6、格式化字符串(format string)

my_name = 'Zed A. Shaw'

my_age = 35 # not a lie

my_height = 74 # inches

my_weight = 180 # lbs

my_eyes = 'Blue'

my_teeth = 'White'

my_hair = 'Brown'

print "Let's talk about %s." % my_name

print "He's %d inches tall." % my_height

print "He's %d pounds heavy." % my_weight print "Actually that's not too heavy."

print "He's got %s eyes and %s hair." % (my_eyes, my_hair)

print "His teeth are usually %s depending on the coffee." % my_teeth # this line is tricky, try to get it exactly right

print "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight)

 

$ python ex5.py

Let's talk about Zed A. Shaw.

He's 74 inches tall.

He's 180 pounds heavy. Actually that's not too heavy.

He's got Blue eyes and Brown hair.

His teeth are usually White depending on the coffee.

If I add 35, 74, and 180 I get 289.

这样定义变量行不行: 1 = 'Zed Shaw'? 不行。 1 不是一个有效的变量名称。变量名要以字母开头。所以 a1 可以,但 1 不行。 %s, %r, %d 这些符号是啥意思? 后面你会详细学到更多,现在可以告诉你的是它们是一种“格式控制工具”。它们告诉 Python 把 右边的变量带到字符串中,并且把变量值放到 %s 所在的位置上。 还是不懂,“格式控制工具”是啥? 要明白一些描述的意义,你得先学会编程才更容易理解,你可以把这样的问题记录下来,看后面的 内容会不会向你解释这些东西。 如何将浮点数四舍五入? 你可以使用 round() 函数,例如: round(1.7333) 我碰到了错误: TypeError: ‘str’ object is not callable。 很有可能你是漏写了字符串和变量之间的 % 。

转载于:https://www.cnblogs.com/chenwu666/p/10813655.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值