Numbers and Expressions
1.0/2.0
=0.5
1/2
=0
another alternative, if you are programming with command line
Large Integers
2147483647 to -2147483648
larger, we need to use longs: L
Hexadecimals and Octals
0xAF=175
010=8
Getting Input from the User
x=input("x:")
Deal with imaginary and floats numbers
import cmath
Strings
print double quotes
" hello, world!"
it shows the same for these cases
but for some cases the double quotes and single quotes make difference
like
"Let's go!"
and '"hello, peter" she said'
examples:
'let's go'
will not work
but 'let\'s go' will work finely
the same as the former:
"\"hello, world\" she said"
will work finely
input vs. raw_input
input suppose that what you have entered is a valid python expression:
name= input("what is your name?")
print "Hello,"+name +"!"
if we input Zhichao, error will come out but "zhichao" will not pop out error.
for raw_input, we will not have such problems.
raw_input treat all input as raw data and puts it into a string.
Slash
like \n = <enter>
if we want to input \ the way to do that is \\
but for long input, this will be a lot of work.
raw strings are used at such kind of cases
print r'C:\nowhere'
but the last character can not be backslash
but what if you need a backslash at last
here is a simple solution:
print r'C:\program\foo\bar' '\\'
function
abs()
cmath.sqrt()
float(): convert the string or number to a floating-point number
help()
input()
int()
long()
math.ceil(number): return the ceiling of a number as float
math.floor(number): floor of a number as a float
math.sqrt()
pow(x,y[,z])
raw_input(pompt)
repr(object) return a string representation of a value
round(number,ndigits]) round a number to a given precision
str(object) convert a value to a string