Skip to main content

Variables in Python

Variables

Variables are like containers holding data from a memory location. 

In different terms, Variable is used to retrieve/store value from/to memory. 

In programming languages, Variables are defined with specific data type and are used to any kind of processing related to the data type. 

How are variables defined in Python? There is no explicit declaration for variables in Python like most other languages. Variable is automatically created the moment the data is assigned. 

How does python know what data type the variable should be created with? Python creates the variables in the run time based on the data assigned and variable is created based on the type of the data is assigned. It is easy to understand with some examples. 

E.g.

Let's assign an integer to 'first_variable' and string to 'second_variable'. 

Variables in Python

How do we know the data type of a variable? 'type' function would return the data type of a variable. 

Let's try to print the data in these two variables along with data type of the variables. 

Variables in Python

  • Line - 6: We are passing first_variable, type(first_variable) to print function. This should print the data stored in the variable and corresponding data type. 
  • Line - 11: We are passing second_variable, type(second_variable) to print function. This should print the data stored in the variable and corresponding data type.
Variables in Python

Type function has returned the data type along with the text 'class'. So, Would it always be the same? How should we check the data type inside the program (instead of printing)?

We still need to use the type function, and compare it with data type we need to check for. 

Variables in Python

  • Line - 13: We are checking if the type of the variable is 'int' and it is not enclosed with in quotes. This would consider this as a data type. Data type shouldn't be enclosed with in quotes (if it is it would consider as string and would execute else part).

Variables in Python

This would be the same for any other data type. Just use 'str' for String, 'float' for Float, 'list' for List...

We now know that Python assigns data type automatically when a value is assigned. What would happen if the data (of different data types) is assigned multiple times?

Let's see,

Variables in Python

Here we have assigned integer (line - 3) and string (line - 8) to first_variable and running print statement after each assignment. 

Variables in Python

We can see that first print statement has printed the data type as 'int' and second print statement has printed the data type as 'str'. So, data type of a variable can be changed based on the value assigned. 

Are there any constraints on what name to be used for a variable? Yes, there are few constraints on how a variable should be named. 

  • Variable name should start with a letter or underscore and cannot start with a number.
  • No special characters are allowed in a variable name (except underscore). 
  • Variable names are case sensitive (E.g.: First and first would be considered as two different variables).


If you have any Suggestions or Feedback, Please leave a comment below or use Contact Form.

Comments

Popular posts from this blog

All about READ in RPGLE & Why we use it with SETLL/SETGT?

READ READ is one of the most used Opcodes in RPGLE. As the name suggests main purpose of this Opcode is to read a record from Database file. What are the different READ Opcodes? To list, Below are the five Opcodes.  READ - Read a Record READC - Read Next Changed Record READE - Read Equal Key Record READP - Read Prior Record READPE - Read Prior Equal Record We will see more about each of these later in this article. Before that, We will see a bit about SETLL/SETGT .  SETLL (Set Lower Limit) SETLL accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Corresponding Record (or Next Record if exact match isn't found).  SETGT (Set Greater Than) SETGT accepts Key Fields or Relative Record Number (RRN) as Search Arguments and positions the file at the Next Record (Greater Than the Key value). Syntax: SETLL SEARCH-ARGUMENTS/KEYFIELDS FILENAME SETGT  SEARCH-ARGUMENTS/KEYFIELDS FILENAME One of the below can be passed as Search...

What we need to know about CHAIN (RPGLE) & How is it different from READ?

CHAIN READ & CHAIN, These are one of the most used (& useful) Opcodes by any RPG developer. These Opcodes are used to read a record from file. So, What's the difference between CHAIN & READ?   CHAIN operation retrieves a record based on the Key specified. It's more like Retrieving Random record from a Database file based on the Key fields.  READ operation reads the record currently pointed to from a Database file. There are multiple Opcodes that start with READ and all are used to read a record but with slight difference. We will see more about different Opcodes and How they are different from each other (and CHAIN) in another article. Few differences to note.  CHAIN requires Key fields to read a record where as READ would read the record currently pointed to (SETLL or SETGT are used to point a Record).  If there are multiple records with the same Key data, CHAIN would return the same record every time. READE can be used to read all the records with the s...

Extract a portion of a Date/Time/Timestamp in RPGLE - IBM i

%SUBDT Extracting Year, Month, Day, Hour, Minutes, Seconds or Milli seconds of a given Date/Time/Timestamp is required most of the times.  This can be extracted easily by using %SUBDT. BIF name looks more similar to %SUBST which is used to extract a portion of string by passing from and two positions of the original string. Instead, We would need to pass a value (i.e., Date, Time or Timestamp ) and Unit (i.e., *YEARS, *MONTHS, *DAYS, *HOURS, *MINUTES, *SECONDS or *MSECONDS) to %SUBDT.  Valid unit should be passed for the type of the value passed. Below are the valid values for each type. Date - *DAYS, *MONTHS, *YEARS Time - *HOURS, *MINUTES, *SECONDS Timestamp - *DAYS, *MONTHS, *YEARS, *HOURS, *MINUTES, *SECONDS, *MSECONDS Syntax: %SUBDT(value : unit { : digits { : decpos} }) Value and Unit are the mandatory arguments.  Digits and Decimal positions are optional and can only be used with *SECONDS for Timestamp. We can either pass the full form for the unit or use the short...