Python学习笔记(1)之python的基本语法

一、python的输入和输出

1、python的输出

  • 使用print()加字符串可以在屏幕中输出指定的字符串,如下
print("Hello World")
  • print()函数可以接受多个字符串输出,中间用逗号隔开,遇到逗号会输出一个空格,故以下输出结果会连成一句话。
print('The quick brown fox', 'jumps over', 'the lazy dog')
//输出结果
The quick brown fox jumps over the lazy dog
  • print()也可以打印打印整数或者计算结果
print('30+10');
print(30+10);
print('30+10=',30+10);
//输出结果
30+10
40
30+10= 40
  • print()采用的格式化方式和C语言是一致的,用%实现,
    示例如下:
print 'Hi, %s, you have $%d.' % ('Michael', 1000000)
#运行结果
Hi, Michael, you have $1000000.

2、python的输入

  • 使用input()可以让用户输入字符串,并存放到一个变量中。
name = input('please enter your name: ')
print('hello,', name)
//输出结果
please enter your name: Corrine
hello, Corrine

二、Python的特点

1、Python不使用中括号

  • 学习 Python 与其他语言最大的区别就是,Python 的代码块不使用大括号 {} 来控制类,函数以及其他逻辑判断。python 最具特色的就是用缩进来写模块
  • 缩进的空白数量是可变的,但是所有代码块语句必须包含相同的缩进空白数量,这个必须严格执行,否则程序会报错。
  • Python语句中一般以新行作为语句的结束符。但是我们可以使用斜杠( \)将一行的语句分为多行显示
total = item_one + \
        item_two + \
        item_three
  • 语句中包含 [], {} 或 () 括号就不需要使用多行连接符。如下实例:
days = ['Monday', 'Tuesday', 'Wednesday',
        'Thursday', 'Friday']
  • python中单行注释采用 # 开头。

参考: 廖雪峰的官方网站.

你可能感兴趣的:(Python学习笔记)