Python for Civil and Structural Engineers-笔记
Python for Civil and Structural Engineers-笔记
With the .format() method python lets you insert variables within a string, in he places specified by the
numbers between the curly brackets. The method .format() can accept multiple arguments.
Example 1:
a=2+2
print(“The result of 2+2 is: {0}”.format(b))
Example 2:
a=12
b=3
print(“a={0} and b={1}”.format(a,b))
String formatting can also be achieved with a different method that uses the % symbol.
Example 1:
a=2.3847567838
b=5
print(“a=%.2f and b=%d”%(a,b))
常用的格式化字符如下表:
格式化字符 说明 格式化字符 说明
%s 字符串(采用 str()显示) %r 字符串(采用 repr()显示)
%c 单个字符 %o 八进制整数
%d 或者 %i 十进制整数 %e 指数(基底写为 e)
%x 十六进制整数 %E 指数(基底写为 E)
%f 或者 %F 浮点数 %% 字符%
The range() function is used to create a sequence of numbers that go from 0 to a certain value, and can be
very useful in for loops as a way to repeat an operation a specific number of times. The range(n) function
creates a sequence that goes from 0 to n-1, NOT from 0 to n or from 1 to n.
For loops are used to iterate through a sequence, be it a list, a string, a dictionary, and so on, or to perform
a repetitive operation a certain number of items. A for loop is introduced with the keyword for, followed
by an iterator, followed by an iterable object. The block of code that comes after that is run again and
again at each iteration of the for cycle.
NumPy:
What library NumPy does is provide the user with a powerful object called ndarray (n-dimensional
array, but generally referred to simply as array) that expands the functionalities of Python quite a bit.
To specify the position of the element in an array: [i,j]; To specify the position of the element in a list: [i]
[j].
Array slicing:
arrayname[start:end:step]
where
arrayname: name of the array
start: beginning location of the slicing (included in the slicing)
end: ending location of the slicing (not included in the slicing)
step: increment of the slicing
SymPy:
Computer Algebra Systems (CAS) allow the user to perform symbolic calculations. What we have seen
so far were only numerical calculations, because when evaluating a formula each variable had a value
assigned to it so that it could return a numerical result.
In Python the most widely used CAS is SymPy. It can store formulas and variables as symbolic
expressions, and perform symbolic operations on them.
Pandas:
The main tool that pandas provides to the user is a new type of structure called a DataFrame. Think of it
like a 2D data structure with headers for both columns and rows. It allows the user to perform all kinds of
operations on the values contained within, from simple slicing to complex interpolation schemes.
The function pandas.DataFrame() allows the user to initialize a data frame column by column. It is easy
to understand how the syntax works: a dictionary (the part contained in the curly brackets) is used to
define the various columns, while the optional parameter index is used to specify the label assigned at
each row. Note that the length of index must be equal to the length of the columns of data.