Python学习入门笔记(一)Print用法

2017-07-12  本文已影响0人  Yao_0

本系类笔记采用的是Python3.5X版本,编程环境为Windows64位下的Anaconda
主要的重点是通过编写正确或错误的Python语句,让读者明白pythond语句的常用用法,省略了知识性的细节。
目的是使有一定其他语言基础(如c++,c等)的人能够通过这些实例进而读懂Python程序,而非系统学习。

注:所有代码部分均为连续的,“结果”为在jupyter分步运行结果

'''
基本数据类型:

integer 
整型(整数类型)
1
5
100

float
浮点型(小数)
0.1
3.1415
189.153

string
字符串型
"This is a string"
"123"
'123'

'''

代码部分:

print("hello world")

hello world

print "hello world" #一定要加括号

File "<ipython-input-3-d0db7f9aba35>", line 1
print "hello world"
^
SyntaxError: Missing parentheses in call to 'print'

print(hello world)

File "<ipython-input-4-63868289fc00>", line 1
print(hello world)
^
SyntaxError: invalid syntax

print(“hello world”)#不能出现中文字符

File "<ipython-input-5-dccc2e36aac4>", line 1
print(“hello world”)
^
SyntaxError: invalid character in identifier

print('hello world')

hello world

print("hello")
print("world")

hello
world

print("hello\tworld")#\t 代表TAB键,会多出4个字符

hello world

print("hello\nworld")#|n 回车键

hello
world

print("hello" + 123)

TypeError Traceback (most recent call last)
<ipython-input-10-4a197b7c56f0> in <module>()
----> 1 print("hello" + 123)

TypeError: Can't convert 'int' object to str implicitly

print("hello" + "123")

hello123

print("hello" + str(123))

hello123

print(1+2)

3

print('1+2')

1+2

print(int('1')+2)//字符串转int再相加

3

print(int('1.5')+2)

ValueError Traceback (most recent call last)
<ipython-input-16-6c694898d605> in <module>()
----> 1 print(int('1.5')+2)

ValueError: invalid literal for int() with base 10: '1.5'

print(float('1.5')+2)

3.5

print("hello world" 123")

File "<ipython-input-18-b0d10b0e9fbe>", line 1
print("hello world" 123")
^
SyntaxError: invalid syntax

上一篇 下一篇

猜你喜欢

热点阅读