Basics Of Python

· Variables:

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:

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:

Arithmetic operators are used with numeric values to perform common mathematical operations:

Assignment operators:

Assignment operators are used to assign values to variables:

Comparison operators:

Comparison operators are used to compare two values:

Logical operators:

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:-

§ Numeric

§ Sequence type

§ Boolean

§ Set

§ Dictionary

Here we will discuss about some of them.

§ Integer:

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.

§ Float:

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.

§ String:

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.

Syntax: if(condition):

statement 1

statement 2

Example:

I. If-else statement:

An if statement can be followed by an optional else statement, which executes when the boolean expression is FALSE.

Syntax: if(condition):

statement 1 #This will execute when the condition is true.

else:

statement 2 #This will execute when the condition is false.

Example:

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.

Syntax: if(condition):

statement 1

elif(condition):

statement 2

.

.

.

else:

statement 3

Example:

· Calculator using Python:

Code:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store