Skip to main content

A Python Show

Hello Readers, We are going to dive into python language which is the most in-demand technology of the current generation.

Introduction

    Python is a widely used high-level programming language. It was created by  Guido van Rossum and first released in 1991.Python is easy to use, powerful, and versatile, making it a great choice for beginners and experts alike. Its unique features allows programmers to easily understand the concepts of python  which makes them love python more than any other programming language.

Features of Python

Python has vast number of features. Let  me list a few among them.

1. Easy to code.

2. Free and Open Source.

3. Object-Oriented Language.

4. GUI Programming Support.

5. High-Level Language.

6. Extensible feature.

7. Python is Portable language.

8. Python is Integrated language.

9. Interpreted Language.

10. Large Standard Library.

11. Dynamically Typed Language.

Variables:

Python variable is a reserved memory location to store values. In other words, a variable in a python program gives data to the computer for processing.

How to create a variable?

To create a variable in Python, all you need to do is specify the variable name, and then assign a value to it.

<variable name>  =  <value>

example:

#integer variable

a=10


#float variable

a=3.5


#string variable

a="Ramu"


Variable assignment works from left to right. So the following will give you an syntax error.

 0 = x 

=> Output

        SyntaxError: can't assign to literal


Rules for Creating a Variable:




1. Variables names must start with a letter or an underscore.

         x = True     # valid 

         _y = True   # valid  

        9x = False  # starts with numeral 

        => SyntaxError: invalid syntax

        $y = False  # starts with symbol 

        => SyntaxError: invalid syntax


2.The remainder of your variable name may consist of letters, numbers and underscores.

        has_0_in_it = "Still Valid"

3. Names are case sensitive. 

          x = 9

         y = X*5

         =>NameError: name 'X' is not defined



Type Function:

    The Python interpreter automatically picks the most suitable built-in type for the variable while declaring it. The type function is used for showing the type of the variable.


      a = 2 

     print(type(a))

     # Output: <type 'int'>


     b = 9223372036854775807 

     print(type(b)) 

     # Output: <type 'int'>


     pi = 3.14 

     print(type(pi)) 

     # Output: <type 'float'>


    c = 'A' 

    print(type(c))

    # Output: <type 'str'>


    name = 'John Doe' 

    print(type(name)) 

    # Output: <type 'str'>



Packing and Unpacking :

You can assign multiple values to multiple variables in one line. Note that there must be the same number of arguments on the right and left sides of the = operator.

 a, b, c = 1, 2, 3 

 print(a, b, c)   # Output: 1 2 3 


a, b, c = 1, 2 

 => Traceback (most recent call last): => File "name.py", line N, in => a, b, c = 1, 2 

=> ValueError: need more than 2 values to unpack 

a, b = 1, 2, 3

 => Traceback (most recent call last): => File "name.py", line N, in => a, b = 1, 2, 3

 => ValueError: too many values to unpack


Strings

Strings in Python are arrays of bytes representing Unicode characters. It can be represented using single quotes as well as double quotes. Strings are immutable.  Python does not have a character data type, a single character is simply a string with a length of 1.



String Slicing:

You can return a range of characters by using the slice syntax. Specify the start index and the end index, separated by a colon, to return a part of the string.

b = "Hello, World!"
print(b[2:5])

Output: llo

Slice From the Start

By leaving out the start index, the range will start at the first character:

Example

Get the characters from the start to position 5 (not included):

b = "Hello, World!"
print(b[:5])
Output: "Hello"

Negative Indexing

Use negative indexes to start the slice from the end of the string:

b = "Hello, World!"
print(b[-5:-2])


Output: orl



String Concatenation:

To concatenate, or combine, two strings you can use the + operator.

a = "Hello"
b = "World"
c = a + b
print(c)


Output: HelloWorld


With Space,

a = "Hello"
b = "World"
c = a +" " +b
print(c)


Output: Hello World


Modification of String:

Strings are immutable.

S="Ramu"

S[1]="o"

TypeError: 'str' object does not support item assignment



>>> del S[1] 

 TypeError: 'str' object doesn't support item deletion .

Comments

Popular posts from this blog

Is-A and Has-A relationships in python

  In object-oriented programming, the concept of IS-A is a totally based on Inheritance, which can be of two types Class Inheritance or Interface Inheritance. It is just like saying "A is a B type of thing". For example, Apple is a Fruit, Car is a Vehicle etc. Inheritance is uni-directional. For example, House is a Building. But Building is not a House. #Is-A relationship --> By Inheritance class  A:    def   __init__ ( self ):      self .b= 10    def   mym1 ( self ):      print ( 'Parent method' ) class  B(A):    def   mym2 ( self ):      print ( 'Child method' ) d = B() d.mym1() #output: Parent method d.mym2() #output: Child method HAS-A Relationship:  Composition(HAS-A) simply mean the use of instance variables that are references to other objects. For example Maruti has Engine, or House has Bathroom. Let’s understand...

Inheritance and Types in Python

  Inheritance   Creating a new class from existing class is known as inheritance . The class from which features are inherited is known as base class and the class into which features are derived into is called derived class . Syntax: class  derived- class (base  class ):       < class -suite>      Inheritance promotes reusability of code by reusing already existing classes.  Inheritance is used to implement  is-a  relationship between classes.   Following hierarchy is an example representing inheritance between classes:   Single inheritance   When a derived class inherits only from syntax, the base class is called single inheritance. If it has one base class and one derived class it is called single inheritance.   Diagram     Syntax class  A:  #parent class         #some code       class  b(A):...

Exception Handling in Python

  Introduction   An error is an abnormal condition that results in unexpected behavior of a program. Common kinds of errors are syntax errors and logical errors. Syntax errors arise due to poor understanding of the language. Logical errors arise due to poor understanding of the problem and its solution.   Anomalies that occur at runtime are known as exceptions. Exceptions are of two types: synchronous exceptions and asynchronous exceptions. Synchronous exceptions are caused due to mistakes in the logic of the program and can be controlled. Asynchronous exceptions are caused due to hardware failure or operating system level failures and cannot be controlled.   Examples of synchronous exceptions are: divide by zero, array index out of bounds, etc.) . Examples of asynchronous exceptions are: out of memory error, memory overflow, memory underflow, disk failure, etc. Overview of errors and exceptions in Python is as follows:     Handling Exceptions   Flowch...