Introduction to Python Basics for PSSE Integration
FarhanKhan978284
72 views
27 slides
Aug 04, 2024
Slide 1 of 27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
About This Presentation
Understanding Python Basics is essential for using to it to create Automation Tools for PSSE. This presentation will help you get the initial start.
Size: 677.01 KB
Language: en
Added: Aug 04, 2024
Slides: 27 pages
Slide Content
PSSE - Python Introduction to Python
What is Python? Python is a popular programming language. It was created by Guido van Rossum, and released in 1991. It is used for: web development (server-side), software development, mathematics, system scripting.
What can Python do? Python can be used on a server to create web applications. Python can be used alongside software to create workflows. Python can connect to database systems. It can also read and modify files. Python can be used to handle big data and perform complex mathematics. Python can be used for rapid prototyping, or for production-ready software development.
Why Python? Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc ). Python has a simple syntax similar to the English language. Python has syntax that allows developers to write programs with fewer lines than some other programming languages. Python runs on an interpreter system, meaning that code can be executed as soon as it is written. This means that prototyping can be very quick. Python can be treated in a procedural way, an object-oriented way or a functional way.
Getting Started Already comes pre-installed with PSSE Python comes with a standard shell (IDLE) Pretty basic Not interactive Many other shells exists – ipython is excellent & recommended Using IDE for code development provides best way for writing complex codes Anaconda Distribution – comes with Spyder IDE (recommended as it comes with pre-loaded data science libraries) Pycharm is another IDE
IDE Installation To install anaconda on your windows machine Go to the link https://www.anaconda.com/products/individual Download the required version for PSSE application we require Anaconda 2.7 32 bit To install Pycharm on your windows machine Go to the link https://www.jetbrains.com/pycharm/download/#section=windows Let us download and set up our IDE & ipython
Let’s Setup Python Environment
Writing First code Python syntax can be executed by writing directly in the Command Line Python Indentation Indentation refers to the spaces at the beginning of a code line. Where in other programming languages the indentation in code is for readability only, the indentation in Python is very important Python uses indentation to indicate a block of code.
Comments Comments can be used to explain Python code. Comments can be used to make the code more readable. Comments can be used to prevent execution when testing code. Comments starts with a # Python does not really have a syntax for multi line comments To add a multiline comment you could insert a # for each line
Declaring Variables Variables are containers for storing data values. Python has no command for declaring a variable. A variable is created the moment you first assign a value to it. You can get the data type of a variable with the type() function String variables can be declared either by using single or double quotes Variable names are case-sensitive
Numbers There are three numeric types in Python int float complex Variables of numeric types are created when you assign a value to them You can convert from one type to another with the int (), float() & complex() method
Specify a Variable Type There may be times when you want to specify a type on to a variable. This can be done with casting. Python is an object-orientated language, and as such it uses classes to define data types, including its primitive types. Casting in python is therefore done using constructor functions int () - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number) float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer) str () - constructs a string from a wide variety of data types, including strings, integer literals and float literals
Strings Strings in python are surrounded by either single quotation marks, or double quotation marks. Assigning a string to a variable is done with the variable name followed by an equal sign and the string Like many other popular programming languages, strings in Python are arrays of bytes representing unicode characters. Python does not have a character data type, a single character is simply a string with a length of 1 Square brackets can be used to access elements of the string
Basic Mathematical Operations Basic math functions like addition, subtraction, multiplication, division can be performed directly Python uses the standard order of operations as taught in Algebra and Geometry classes at high school or secondary school. That is, mathematical expressions are evaluated in the following order (memorized by many as PEMDAS), which is also applied to parentheticals.
Math Library for Trigonometric Functions The Python Math Library provides us access to some common math functions and constants in Python, which we can use throughout our code for more complex mathematical computations. The library is a built-in Python module, therefore you don't have to do any installation to use it. Some common trigonometric functions are
Math Library for Arithmetic Functions Arithmetic functions are used to represent numbers in various forms and perform mathematical operations on them. Some common arithmetic functions are
Array Collections There are four collection data types in the Python programming language List is a collection which is ordered and changeable. Allows duplicate members. Tuple is a collection which is ordered and unchangeable. Allows duplicate members. Set is a collection which is unordered and unindexed. No duplicate members. Dictionary is a collection which is unordered and changeable. No duplicate members.
Lists Lists are used to store multiple items in a single variable. List is a collection which is ordered and changeable. Allows duplicate members. Lists are created using square brackets One the most usable feature, which is frequently used in PSSE
Dictionary Dictionaries are used to store data values in key:value pairs A dictionary is a collection which is ordered* , changeable and does not allow duplicates Dictionaries are written with curly brackets, and have keys and values You can access the items of a dictionary by referring to its key name, inside square brackets You can change the value of a specific item by referring to its key name Adding an item to the dictionary is done by using a new index key and assigning a value to it
Practice Write a program to calculate per unit value of transmission lines from given actual values and length Assume the following data Basekv = 132 Base MVA = 100 R = 0.02 Ω /km X = 0.1 Ω /km B = 2.0 µS/km Length = 30km
Control Flow Statements : Looping – For loop A for loop is used for iterating over a sequence
Control Flow Statements : Looping – While loop With the while loop we can execute a set of statements as long as a condition is true.
Control Flow Statements : If Statement
Control Flow Statements : Loop Control Statements Break statement: It stops executing and moves to the first statement after the loop Continue statement: It skips all statement after ‘continue’. Then, it shifts to the next item in the sequence. Pass statement: It is used when a statement is required syntactically but you do not want any command or code to execute.
Practice Write a program to identify buses with voltages greater than 1.05 per unit busnum =[200,300,400,500,600,700] busvoltage =[128,139,132,140,130,145] Base Voltage = 132kV
Input/Output Processing
Functions Block of organized and reusable code It is used to perform a single, related action. Built in function & user-defined function