Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Time for action – assigning values to variables


Assigning values to variables in Python works in a similar way to most programming languages.

  1. For instance, assign the value of 2 to a variable named var as follows:

    >>> var = 2
    >>> var
    2
    
  2. We defined the variable and assigned it a value. In this Python code, the type of the variable is not fixed. We can make the variable in to a list, which is a built-in Python type corresponding to an ordered sequence of values. Assign a list to var as follows:

    >>> var = [2, 'spam', 'eggs']
    >>> var
    [2, 'spam', 'eggs']
    

    We can assign a new value to a list item using its index number (counting starts from 0). Assign a new value to the first list element:

    >>> var
    ['ham', 'spam', 'eggs']
    
  3. We can also swap values easily. Define two variables and swap their values:

    >>> a = 1
    >>> b = 2
    >>> a, b = b, a
    >>> a
    2
    >>> b
    1
    

What just happened?

We assigned values to variables and Python list items. This section is by no means exhaustive; therefore, if you are struggling, please read Appendix B, Additional Online Resources, to find recommended Python tutorials.