Introduction To Python Basics
Introduction To Python Basics
Outline
What is Python
Python Programming
What is Python?
Python is a object-oriented, interpreted, and interactive programming language
Recommended References
Free materials http://www.python.org/ Python download all formats Tutorial and documentation http://ironpython.codeplex.com/
IronPython studio is free Microsoft SDK that allows you to manage your projects and develop using native windows forms
Python Editors
Notepad++ Supports several languages (Python, Jscript, XML, ) IDLE Standard Python editor that comes with Python. Good for most small programs.
Invoke the ipy.exe in command prompt to have a Python console Path: C:\Program Files\ANSYS Inc\v145\commonfiles\IronPython\ipy.exe
!
6
You can also just drag and drop the ipy.exe file to a standard command prompt
November 29, 2012
Tips:
Python Programming
Variables
A variable:
Variable Types:
Syntax
Colon
Semicolon
Don't terminate all of your statements with a semicolon(;) It is not required unless you're placing more than one
statement on a single line
Whitespace
Comments
Syntax # 1: # Comment
11
Text after # is ignored in a single line Syntax # 2: Comment Text between pairs is ignored Python is case-sensitive Variable names may mix upper- and lower-case Calling code must match the case exactly Python keywords are case-sensitive too
2011 ANSYS, Inc. November 29, 2012
Case-sensitivity
Operators
Arithmetic operators
Relational operators
Comparisons return Boolean values Equal to, not equal to Less than, greater than Less than or equal to, greater than or equal to
Membership operators
To test for membership in a sequence, such as strings, lists, or tuples in, not in
Identity Operators
Flow Control
If-Else conditional
Variations
if, if...else, elif, nested if Conditions Boolean or non-zero String Comparison Complex comparison Indentation: The level of indentation determines the end of a condition Colon: Required at the end of compound statement headers
2011 ANSYS, Inc. November 29, 2012
13
for loop
14
continue statement
else statement
pass statement
Functions
Function declaration
begin with the keyword def followed by the function name and parentheses
Input parameters or arguments should be placed within these parentheses
Numbers
Python supports four different numerical types:
Mathematical Functions
abs(x), min(x1, x2, ..), Trigonometric Functions Import math module to avail these sin(x), acos(x), Random Functions
Mathematical Constants
Strings
Strings are stored as a list of single characters strings Built-in String Methods
String Formatting
Variables and strings can be combined, using formatting characters. This works also within a print statement. In both cases, the number of values and formatting characters must be equal.
Escape characters
Strings may contain also the symbols: \t (tabulator), \n (newline), \r (carriage return),
and \\ (backslash) To read and use a staring containing special characters, use r before string E.g. filePath = rE:\temp # w/o the r, it will be treated as E: *tab] emp
19 2011 ANSYS, Inc. November 29, 2012
Lists
The list is a most versatile datatype available in Python
List modification
Dictionaries
The Dictionary is a container type that can store any number of Python objects, including
other container types Dictionaries consist of pairs (called items) of keys and their corresponding values Also known as associative array or hash table Each key is separated from its value by a colon (:) Items are separated by commas whole thing is enclosed in curly braces
Syntax:
Accessing values
Use square brackets along with the key to obtain its value Trying to access a data item with a key which is not part of the dictionary, gives error
21
Defining a Class
The class statement creates a new class definition
The first method __init__() called class constructor or initialization method Creating an instance objects:
arguments its __init__ method accepts self points to the new object Use self to define properties for your class
23
Modules
A module is a file containing Python definitions and statements
A module can define functions, classes, and variables You can benefit from different modules by importing them to your code!
import Statement:
Imports a module It is customary but not required to place all import statements at the
beginning of the code
from...import Statement
24
25
Exceptions
Exceptions are the Python mechanism to make sure that code do not blow up in the wrong moment, so that important data is saved Except, else and finally:
Typical Exceptions:
26
File Operations
Opening files for reading
Text files can be accessed using the open() function. It returns an open file from which its
contents can be extracted as a string. f = open('my_file.txt') text = f.read()
Writing text to files is very similar. The only difference is the 'w' parameter.
Appending to files
Closing files
f.close()
27 2011 ANSYS, Inc. November 29, 2012
Replace the backslash '\' by a double backslash (because '\' is also used for '\n' and '\t')
f = open('..\\my_file.txt') f = open('C:\\python\\my_file.txt')
28
29
Command Window
Command Window supports:
Command completion Command history Keyboard shortcuts for cursor navigation and editing
30
31
System: A collection of components that together provide a workflow to achieve an engineering simulation goal. Systems are created from System Templates.
32
Component: A collection of data and a data editor that work together to achieve a CAE-related task. A Component is represented by a Cell in the Workbench Project Schematic.
33
Component Data Container: Data unique to an individual component, and the services to manage and manipulate it.
34
Data Entity: A data structure defined within a data container. A data container often has several data entities.
35
36
Try out
Record a journal from WB and using any text editor, extract the user inputs to the top Replay the journal file
37