Basics Of Python
Variables are like containers which can store different kind of data values. Python has no command for declaring a variable. A variable will be automatically created when the user will assign a value to it.
Variables don’t need to be declared with any particular type and can even change type after they have been set.
If you want to specify the data type of a variable, this can be done with casting.
Variable names are case sensitive. Here, this will create two different variables.
Operators are used to perform various operations on values and variables. There are total 7 types of operators.
I. Arithmetic operators
II. Assignment operators
III. Comparison operators
IV. Logical operators
V. Identity operators
VI. Membership operators
VII. Bitwise operators
Among all of these we will discuss about 4 of them here :
Arithmetic operators are used with numeric values to perform common mathematical operations:
Assignment operators are used to assign values to variables:
Comparison operators are used to compare two values:
Logical operators are used to combine conditional statements:
· Data types:
Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. The built-in data types are as follows:-
§ Sequence type
Here we will discuss about some of them.
This value is represented by int class. It contains positive or negative whole numbers (without fraction or decimal). In Python there is no limit to how long an integer value can be.
This value is represented by float class. It is a real number with floating point representation. It is specified by a decimal point. Optionally, the character e or E followed by a positive or negative integer may be appended to specify scientific notation.
In Python, Strings are arrays of bytes representing Unicode characters. A string is a collection of one or more characters put in a single quote, double-quote or triple quote. In python there is no character data type, a character is a string of length one. It is represented by str class.
· Decision making in Python:
Decision making is anticipation of conditions occurring while execution of the program and specifying actions taken according to the conditions.
Decision structures evaluate multiple expressions which produce TRUE or FALSE as outcome. You need to determine which action to take and which statements to execute if outcome is TRUE or FALSE otherwise.
Python provides following types of decision making statements.
I. If statements:
An if statement consists of a Boolean expression followed by one or more statements.
I. If-else statement:
An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.
statement 1 #This will execute when the condition is true.
statement 2 #This will execute when the condition is false.
I. If-elif-else ladder:
Here, a user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
· Calculator using Python: