Back





What is Python

Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn't specialized for any specific problems.


print()

- This command is used to show something as output.

Example :

Code :



Output :




Comment

Comments can be used to explain Python code, to make the code more readable, to prevent execution when testing code.
Comments starts with a #, and Python will ignore them.


Example :

Code :




Output :



Note: Comments can be placed at the end of a line, and Python will ignore the rest of the line. A comment does not have to be text that explains the code, it can also be used to prevent Python from executing code.


Code :




Output :






Variable

- Generally, we know that many things change around us. Like, Climate,Your age,Your bank balance, etc.
In maths, we use x & y as variables if we don't know the accurate number.The same thing is applied in the programming too.

Example:
var_name = 151
any_name = "NEAN PROGRAMMING"

The name of the variable must always start with either a letter or an underscore (_). For example: _str, str, num, _num are all valid name for the variables. The name of the variable cannot start with a number. For example: 5num is not a valid variable name.


Note: Variable names are case-sensitive (page, Page and PAGE are three different variables)


Example :

Code :




Output :

We can store any text too.


Code:





Output :


If you want to specify the data type of a variable, this can be done with casting.

Example :

Code :




You can get the data type of a variable with the type() function.

Example :

Code :




Note: String variables can be declared either by using single or double quotes


Python allows you to assign values to multiple variables in one line.

Example :

Code :




Output :






Data types

In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things. Python has the following data types built-in by default, in these categories:

Text Type: str
Numeric Types: int, float, complex
Sequence Types: list, tuple, range
Mapping Type: dict
Set Types: set, frozenset
Boolean Type: bool
Binary Types: bytes, bytearray, memoryview
None Type: NoneType

Python Numbers

There are three numeric types in Python:

1.int
2.float
3.complex

Variables of numeric types are created when you assign a value to them.



Python Strings

Strings in python are surrounded by either single quotation marks, or double quotation marks.
'hello' is the same as "hello".


You can assign a multiline string to a variable by using three quotes.

Example :

Code :




The upper() method returns the string in upper case:


Example :

Code :




Output :



The lower() method returns the string in lower case:


Example :

Code :




Output :



The replace() method replaces a string with another string


Example :

Code :




Output :






List

Lists are used to store multiple items in a single variable.
Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
Lists are created using square brackets:



List is a collection which is ordered and changeable. Allows duplicate members.
Tuple is a collection which is ordered and unchangeable. Allows duplicate members.
Set is a collection which is unordered, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.

*Set items are unchangeable, but you can remove and/or add items whenever you like.
**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.

Code :



Output :



List items are ordered, changeable, and allow duplicate values. List items are indexed, the first item has index [0], the second item has index [1], the third item has index [2], the fourth item has index [3] etc.

Example :

Code :



Output :



Note: A list can contain different data types:
list1 = ["nean", 151, True, 100, "cat"]




input

In Python, we use input() function to take input from the user. Whatever you enter as input, the input function converts it into a string. If you enter an integer value still input() function convert it into a string.


Example :

Code :




Output :



Code :




Output :




Casting

There may be times when you want to specify a type on to a variable. This can be done with casting.


Casting in python is therefore done using constructor functions:


int() - constructs an integer number from an integer literal, a float literal (by removing all decimals), or a string literal (providing the string represents a whole number)
float() - constructs a float number from an integer literal, a float literal or a string literal (providing the string represents a float or an integer)
str() - constructs a string from a wide variety of data types, including strings, integer literals and float literals


Example :

Code :













Conditions if-elif-else

Conditions and If statements


Python supports the usual logical conditions from mathematics:


  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

  • These conditions can be used in several ways, most commonly in "if statements" and loops. An "if statement" is written by using the if keyword.


    Example :

    Code :




    Output :

    In this example we use two variables, a and b, which are used as part of the if statement to test whether b is greater than a. As a is 100, and b is 151, we know that 151 is greater than 100, and so we print to screen that "b is greater than

    Note: Python relies on indentation (whitespace at the beginning of a line) to define scope in the code. Other programming languages often use curly-brackets for this purpose.
    If statement, without indentation will raise an error.


    Elif


    The elif keyword is pythons way of saying "if the previous conditions were not true, then try this condition".




    Output :


    In this example a is equal to b, so the first condition is not true, but the elif condition is true, so we print to screen that "a and b are equal".


    Else


    The else keyword catches anything which isn't caught by the preceding conditions.




    Output :


    In this example a is greater than b, so the first condition is not true, also the elif condition is not true, so we go to the else condition and print to screen that "a is greater than b".




    More Coming Soon!