• No results found

Variables and assignment statements

In document PythonScriptsForAbaqus_BookPreview (Page 50-55)

Running a Script

6. Now if you go back to the command prompt and type path, you see the path has been updated to include Abaqus

3.3 Variables and assignment statements

Python 101

3.1 Introduction

In the cantilever beam example of Chapter 1, we began by creating the entire model in Abaqus/CAE. We then opened up a new file and ran a script which accomplished the exact same task. How exactly did the script work and what did all those code statements mean? Before we can start to analyze this, it is necessary to learn some basic Python syntax. If you have any programming experience at all, this chapter should be a breeze.

3.2 Statements

Python is written in the form of code statements as are other languages. However you do not need to put a semi-colon at the end of each statement. What the Python interpreter looks for are carriage returns (that’s when you press the ENTER key on the keyboard).

As long as you hit ENTER after each statement so that the next one is on a new line, the Python interpreter can tell where one statement ends and the other begins.

In addition statements within a code block need to be indented, such as statements inside a FOR loop. In languages such as C++ you use curly braces to signal the beginning and end of blocks of code whereas in Python you indent the code. Python is very serious about this, if you don’t indent code which is nested inside of something else (such as statements in a function definition or a loop) you will receive a lot of error messages.

Within a statement you can decide how much whitespace you wish to leave. So a=b+c can be written as a = b + c (notice the spaces between each character)

3.3 Variables and assignment statements

In some programming languages such as C++ and Java, variables are strongly typed. This means that you don’t just name a variable; you also declare a type for the variable. So for

example i

stores the Let’s wor

Python is no holds, you sim

dn’t tell Pyth ariable x in Py

on Python doe oat. Some lan also able to r

second one. Y following line

to create an in something lik

t strongly typ mply give it a hon, it would ython and ass

esn’t mind if nguages objec

recognize Str

o’ 

in the variab example to u bles

E. In the lowe look to the e other for “K

You see the k es, hitting the

3.3 Va

nteger variab ke the followi

ped. This mea a name. It cou figure it out ign it a value

you try to do ct to this type ring variables e ENTER key

ariables and

le ‘x’ in C++

ing:

ans you don’t uld be an inte on its own.

of 5 you wou

o things like m e of mixing a , and concate

.

me of these c

window belo message area mand Line Inte

and prompt wh y on your keyb uld simply wr

multiplying a and require a enates them if

oncepts.

ow the viewp you see two erface”.

hich is a “>>>

board after ea

statements

a whole numb an explicit ca f you add them

42 Pytho

ee the Python without you n

their product

ou had combi

= 3.5  e = length * 

volume 

ut is 140.0 . with decimal wo of its facto

idth 

played. Since s 40. The pr g image displ

n interpreter eeding to spe t to the variab

ined integers

width * heig

Note the “.0 point in laym rs ‘length’ an

we set length rint statemen lays what you

realized that ecify what typ ble ‘area’, it

and floats? A

ght 

0” at the end man terms), t nd ‘width’ are

h to 10 and w nt displays th u should see o

the variables pe of variable

decided for i

Add on the fol

. Since your the volume v e integers.

width to 4, the he value stor on your own s

s ‘length’ an es they are. In itself that ‘are

llowing statem

height variab variable is als

e area being t red in the ar screen.

nd ‘width’ sto n addition wh

ea’ was also

Let’s expe variable a objects ar first let’s 3.4 Lis the list va that you c list. This must be o

 le

 ap

eriment with

_name = “Gaut name = “Puri”

= first_name 

ut is “Gautam me’ are String

Python conca

an tell from th o type code in when dealin as being an o re, you will fi

talk about list sts

re a common so I expect y quired to use t

on in similar nown as a Lis

ou store mult ariable follow can store all k is different fr f the same da en() – returns ppend(x) – ad

Strings. Add

tam” 

” 

+ last_name

mPuri”. Notic variables, it atenated them

his example, n Python than ng with instan object of a c ind out in the ts and diction

collection d you’ve dealt w them to write ata type. Lists

the number o dds x to the en

the following

ce that we d figured it ou m together.

not having to n in a langua nces of classe class. If you e section on “ naries.

ata type in ju with them be

Abaqus scrip in your scri

s or data value dex in square types, such a es such as C, s have many b of elements in nd of the list m

g lines

id not tell Py ut on its own.

o define varia ge such as C es so that you

don’t know

“Classes” a fe

ust about eve efore and kno pts, but chanc

ipts. Let’s ex

es and can ref brackets []. T as integers, flo

C++ and Jav built-in functi n the list

making it the

ython that ‘f . Also when w

able types ma C++. This also

u don’t have what classes ew pages dow

ery high leve ow why they ces are you wi

xplore a coll

fer to them w The lowest in oats, Strings, va where all ions, some of

last element

3.4 Lists

first_name’ an we added the

akes it a lot le o saves a lot to define ea , instances an wn the line. B

el programmin

’re useful. Y ill want to sto lection type

with the name

44 Python 101

 remove(y) – removes the first occurrence of y in the list

 pop(i) – removes the element at index [i] in the list, also returns it as the return value

Let’s work through an example.

Example 4.2 - Lists

In the ‘Kernel Command Line Interface’ tab of the lower panel of the window, type in the following statements hitting ENTER after each.

>>>random_stuff = ['car', 24, 'bird' , 78.5, 44, 'golf'] 

>>> print random_stuff[0] 

>>> print random_stuff[1] 

>>> print random_stuff 

>>> print len(random_stuff)   

>>> random_stuff.insert(2, ‘computer’) 

>>> print len(random_stuff) 

>>> print random_stuff 

>>> random_stuff.append(29) 

>>> print len(random_stuff) 

>>> print random_stuff 

>>> random_stuff.remove(24) 

>>> print random_stuff   

>>> removed_var = random_stuff.pop(2) 

>>> print removed_var 

>>> print random_stuff 

Your output will be as displayed the following figure. Note that the lowest index is 0, not 1, which is why random_stuff[0] refers to the first element ‘car’. The len() function returns the number of elements in the list. The append() function adds on whatever is passed to it as an argument to the end of the list. The remove() function removes the element that matches the argument you pass it. And the pop() function removes the element at the index position you pass it as an argument.

3.5 Dic

In document PythonScriptsForAbaqus_BookPreview (Page 50-55)