Advertisement Header

Monday, 30 January 2017

What's the difference between list, tuples and dictionaries in Python?

Lists

Lists are what they seem - a list of values. Each one of them is numbered, starting from zero - the first one is numbered zero, the second 1, the third 2, etc. You can remove values from the list, and add new values to the end.

Example: Your many cats' names.

[code]
"A list"
cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester']
"If you want to print for example the first name on the list you would do the following"
print cats[0]
"Also if you want to add a name to the list you would do this"
cats.append('Catherine')
"To remove a name you would do this"
del cats[0]
[/code]

Tuples

Tuples are just like lists, but you can't change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero, for easy reference.

Example: the names of the months of the year.

[code]
"Here is an example of a tuple"
months = ('January','February','March','April','May','June',\
'July','August','September','October','November',' December')
[/code]

"So that’s the difference between lists and tuples. A list can be modified but a tuple cannot be modified in anyway(unless changed in the source code of the program)"

Dictionaries:

Dictionaries are similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - tare similar to what their name suggests - a dictionary. In a dictionary, you have an 'index' of words, and for each of them a definition. In python, the word is called a 'key', and the definition a 'value'. The values in a dictionary aren't numbered - they aren't in any specific order, either - the key does the same thing. You can add, remove, and modify the values in dictionaries.

Example: telephone book.

[code]
"Here is a simple phonebook"
phonebook = {'Andrew Parson':8806336, \
'Emily Everett':6784346, 'Peter Power':7658344, \
'Lewis Lame':1122345}
"To add a name to the phonebook you would do the following"
phonebook['Gingerbread Man'] = 1234567
"If you want to remove a name you would do the same as you would do in a list"
del phonebook['Andrew Parson']
[/code]

How to describe instances from all regions using AWS CLI

for region in `aws ec2 describe-regions --output text | cut -f3`
do
echo -e "\nListing Instances in region:'$region'..."
aws ec2 describe-instances --region $region
done
As of Today (30-01-2017), You will get list of instances from below 14 regions are available in AWS
ap-south-1
eu-west-2
eu-west-1
ap-northeast-2
ap-northeast-1
sa-east-1
ca-central-1
ap-southeast-1
ap-southeast-2
eu-central-1
us-east-1
us-east-2
us-west-1
us-west-2 

Saturday, 6 August 2011

Windows PowerShell Profiles

Applies To: Windows PowerShell 2.0
When you add aliases, functions, and variables, you are actually adding them only to the current Windows PowerShell session. If you exit the session or close Windows PowerShell, the changes are lost.
To retain these changes, you can create a Windows PowerShell profile and add the aliases, functions, and variables to the profiles. The profile is loaded every time that Windows PowerShell starts.
To load a profile, your Windows PowerShell execution policy must permit you to load configuration files. If it does not, the attempt to load the profile fails and Windows PowerShell displays an error message.

Understanding the Profiles

You can have four different profiles in Windows PowerShell. The profiles are listed in load order. The most specific profiles have precedence over less specific profiles where they apply.
  • %windir%\system32\WindowsPowerShell\v1.0\profile.ps1

    This profile applies to all users and all shells.
  • %windir%\system32\WindowsPowerShell\v1.0\ Microsoft.PowerShell_profile.ps1

    This profile applies to all users, but only to the Microsoft.PowerShell shell.
  • %UserProfile%\My Documents\WindowsPowerShell\profile.ps1

    This profile applies only to the current user, but affects all shells.
  • %UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

    This profile applies only to the current user and the Microsoft.PowerShell shell.

Creating a Profile

When you create or import variables, aliases, or functions, or add a Windows PowerShell snap-in, these elements are added only to the current session. If you exit the session or close the window, they are gone.
To save the variables, aliases, functions, and commands that you use routinely, and make them available in every Windows PowerShell session, add them to your Windows PowerShell profile.
You can also create, share, and distribute profiles to enforce a consistent view of Windows PowerShell in a larger enterprise.
Windows PowerShell profiles are not created automatically. To create a profile, create a text file with the specified name in the specified location. Typically, you will use the user-specific, shell-specific profile, known as the Windows PowerShell user profile. The location of this profile is stored in the $profile variable.
 To display the path to the Windows PowerShell profile, type:

$profile
 To determine whether a Windows PowerShell profile has been created on the system, type:

test-path $profile
If the profile exists, the response is True; otherwise, it is False.
 To create a Windows PowerShell profile file, type:

new-item -path $profile -itemtype file -force
 To open the profile in Notepad, type:

notepad $profile
 To create one of the other profiles, such as the profile that applies to all users and all shells, type:

new-item -path $env:windir\System32\WindowsPowerShell\v1.0\profile.ps1`
 -itemtype file -force

The profile is effective only when the file is located exactly in the path and with the file name that is stored in the $profile variable. Therefore, if you create a profile in Notepad and then save it, or if you copy a profile to
your system, be sure to save the file in the path and with the file name specified in the $profile variable.
If you create a profile in Notepad, enclose the file name in quotation marks to preserve the PS1 file name extension. For example:

"Microsoft.PowerShell_profile.ps1"
Without the quotation marks, Notepad appends the .txt file name extension to the file, and Windows PowerShell will not recognize it.
Use the profile to store the aliases, functions, and variables that you use routinely. One very helpful opens your user profile in your favorite text editor. For example, the following command creates a function called pro that opens the user profile in Notepad.

function pro { notepad $profile }
A well-designed profile can make it even easier to use Windows PowerShell and to administer your system.

The ISE, that comes with V2, has additional Profile Files:

$env:UserProfile\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

$env:windir\system32\WindowsPowerShell\v1.0\Microsoft.PowerShellISE_profile.ps1