Home  >  Article  >  Backend Development  >  What are the basic concepts of Python?

What are the basic concepts of Python?

王林
王林forward
2023-08-31 22:09:101365browse

What are the basic concepts of Python?

Python is a general-purpose interpreted, interactive, object-oriented, and high-level programming language.

Features of Python

The following are the main features of Python:

  • Python supports functional and structured programming methods as well as object-oriented programming.

  • It can be used as a scripting language or can be compiled to byte-code for building large applications.

  • It provides very high-level dynamic data types and supports dynamic type checking.

  • It supports automatic garbage collection.

Variables in Python

Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. Let’s create a variable.

a = 10

Above, a is a variable, assigned a value of 10.

Numeric Datatype in Python

Number data types store numeric values. They are immutable data types, means that changing the value of a number data type results in a newly allocated object.

Python supports four different numerical types.

  • int (signed integers) − They are often called just integers or ints, are positive or negative whole numbers with no decimal point.

  • long (long integers ) − Also called longs, they are integers of unlimited size, written like integers and followed by an uppercase or lowercase L.

  • float (floating point real values) − Also called floats, they represent real numbers and are written with a decimal point dividing the integer and fractional parts. Floats may also be in scientific notation, with E or e indicating the power of 10 (2.5e2 = 2.5 x 102 = 250).

  • complex (complex numbers) − are of the form a bJ, where a and b are floats and J (or j) represents the square root of -1 (which is an imaginary number ). The real part of the number is a, and the imaginary part is b. Complex numbers are not used much in Python programming.

Strings in Python

String is one of the most popular types in Python. We can create them simply by enclosing the characters in quotes. Python treats single and double quotes the same. Creating a string is as simple as assigning a value to a variable.

Let’s see how to easily create a String in Python.

myStr = Thisisit!'

Lists in Python

List is the most versatile data type in Python. You can use square brackets to write comma-separated values ​​(items) into a list. Let's see how to create different types of lists.

myList1 = ['abc', 'pq'];
myList2 = [5, 10, 15, 20];

Tuples in Python

Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values ​​between parentheses also. Let’s see how to create a Tuple.

myTuple1 = ('abc', 'pq)];
myTuple2 = (5, 10, 15, 20);

Dictionary in Python

A dictionary is a sequence in Python. In a dictionary, each key is separated from its value by a colon (:), items are separated by commas, and the entire dictionary is enclosed in curly braces. Keys in a dictionary are unique, while values ​​may not be unique. The values ​​of a dictionary can be of any type, but the keys must be immutable data types, such as strings, numbers, or tuples.

Let’s see how to create a Dictionary −

# Creating two Dictionaries
dict1 = {'Player':['Jacob','Steve','David','John','Kane'], 'Age':[29, 25, 31, 26, 27]}
dict2 = {'Rank':[1,2,3,4,5], 'Points':[100,87, 80,70, 50]}

Classes & Objects in Python

A class is a user-defined object prototype that defines a set of properties that describe any object of that class. These properties are data members and methods, accessed through dot notation.

An object is a unique instance of a data structure that's defined by its class. An object comprises both data members (class variables and instance variables) and methods.

Functions in Python

function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing.

The function block starts with the keyword def, followed by the function name and brackets ( ( ) ). Let's create a function.

def demo(s):
   print (s)
   return
# Function call
demo("Function Called")

Output

Function Called

The above is the detailed content of What are the basic concepts of Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete