Python : Variables, Operators, Expressions and Statements

Variables

As the name implies, a variable is something which can change. A variable is a way of referring to a memory location used by a computer program. A variable is a symbolic name for this physical location. This memory location contains values, like numbers, text or more complicated types.

Please note important point for Python. It does not have a command for declaring a variable. A variable is created the moment you first assign a value to it.

first_Name = 'your name'
y ='techTrekk'
password1 ='password string'
n00b = 'ac'
un_der_scores = 'sample text'
Variables Naming convention
  • Variables names must start with a letter or an underscore but not with number
  • A variable name cannot start with a number
  • A variable name can contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
  • Names are case sensitive.
    casesensitive, CASESENSITIVE, case_sensitive and Case_Sensitive are each a different variable.
Variable naming

See Python PEP 8.

Function names should be lowercase, with words separated by underscores as necessary to improve readability. mixedCase is allowed only in contexts where that’s already the prevailing style

I personally Like camelCase/mixedCase used in JavaScript and hence I prefer to use the same in Python but you can take a call by yourself but make sure you stick with one style to maintain consistency.

Variable name chosen should be “mnemonic”, meaning, by variable name itself, reader should be able to understand what this variable is for. e.g. It is not convenient to store first name in first_name instead of abx123 or abc

Operators

Operators are special symbols that represent computations like additions and subtraction. The value these operators are applied to are called as operands.

Activity Symbol
Addition +
Subtraction
Multiplication *
Division /
Quotient //
Reminder %

Python follows PEDMAS rule

Expressions

An express is combination of values, variable and operators

>>> 1+4
5  

Statements

A statement is used to form the sequence of a program (e.g. if-then , while-do statements).

A statement can be simple or complex and can contain 0 or more expression

Comments

As program becomes bigger, it becomes difficult to track which part is doing what. For this reason, it is a good idea to add notes to the program. Notes can be added as a comments. Comment does not get execute or compile in program.  Comments in python can be added two ways.

Hash Sign (# )

Anything mention after hash symbol(#) is considered as comment. This is limited to the same line where this symbol is added

MULTI LINE comments

Multi line comments can be added by triple double inverted commas. To close the comment, triple inverted comma’s needs to be added again. This method is primarily used for function clarification.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.