0% found this document useful (0 votes)
243 views

Python for Civil and Structural Engineers-笔记

The document provides an overview of Python basics including string formatting using .format() and % methods, the range() function, for loops, and NumPy, SymPy, and Pandas libraries. It explains how to insert variables in strings, create sequences of numbers with range(), iterate through iterables with for loops, perform mathematical operations on NumPy arrays, use SymPy for symbolic calculations, and initialize Pandas DataFrames from NumPy arrays and lists.

Uploaded by

dapinmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
243 views

Python for Civil and Structural Engineers-笔记

The document provides an overview of Python basics including string formatting using .format() and % methods, the range() function, for loops, and NumPy, SymPy, and Pandas libraries. It explains how to insert variables in strings, create sequences of numbers with range(), iterate through iterables with for loops, perform mathematical operations on NumPy arrays, use SymPy for symbolic calculations, and initialize Pandas DataFrames from NumPy arrays and lists.

Uploaded by

dapinmin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Basics:

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 浮点数 %% 字符%

Membership operator in and not in

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

NumPy performs mathematical operations between arrays in an element-wise fashion.

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.

Creating a dataframe from a NumPy array:


Example 1:
import pandas as pd
import numpy as np
A=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
df1=pd.DataFrame(A,columns=[“A”,”B”,”C”,”D”])

Creating a dataframe from a list:


Example 1:
import pandas as pd
import numpy as np
A=[[1,2,3],[4,5,6],[7,8,9]]
df1=pd.DataFrame(A,columns=[“A”,”B”,”C”])

You might also like