田辺  桃子

田辺 桃子

1680564060

Python 中的整数

本教程涉及基本实践之一——Python 中的舍入数字。四舍五入是人们用来简化计算的基本但必不可少的数学函数。其目的是以更简单的形式获取接近实际值的值。

本教程提供了有关 Python 中舍入数字的深入指南。您事先需要的只是一个 IDE 或一个编辑器(我们使用的是 VS 代码)和 Python 3.9.0 或更高版本。

您可能希望通过多种方式对数字进行四舍五入。Python 提供了很多方法来做到这一点。我们将在下面探索所有这些。

舍入 VS 截断

四舍五入将一个值近似为最接近的数字,这在一定程度上简化了计算,同时保持了精度。

同时,截断不考虑任何条件,丢弃指定的小数位数后的数字。

当然,当价值观存在微小差异时,这可能会产生巨大差异。股票市场的例子更能说明问题。

温哥华证券交易所 (VSE) 因在 1982 年每月损失 25 点而臭名昭著,因为他们每天多次将值截断至小数点后三位。让我们考虑以下代码:

import random 
randomValues = [random.uniform(0.01, 0.1) for i in range(500000)]
def truncateNumber(num):
    return int(num * 1000) / 1000
sumOfNumbers = 0
sumOfTruncate = 0
for n in randomValues:
    sumOfNumbers = sumOfNumbers + n
    sumOfTruncate = truncateNumber(sumOfTruncate + n)
print("Truncating upto 3 decimal places:")
print("Original Sum = ", sumOfNumbers)
print("Total Using Truncate = ", sumOfTruncate)
print("Difference (truncate) = ", sumOfNumbers - sumOfTruncate, '\n')           
print("Using round() upto 3 decimal places:")
sumOfNumbers1 = 0
sumOfTruncate1 = 0
for n in randomValues:
    sumOfNumbers1 = sumOfNumbers1 + n       
    sumOfTruncate1 = round(sumOfTruncate1 + n, 3)
print("Original Sum = ", sumOfNumbers1)

print("Total Using round() = ", sumOfTruncate1)
print("Difference (round()) = ", sumOfNumbers1 - sumOfTruncate1)

让我们分解代码以使其更全面:

  1. 我们导入了random 库,使用它我们使用 random.uniform() 启动了一个介于 0.01 和 0.1 之间的随机数数组。这些在 500,000 秒的范围内。
  2. 接下来,truncate(num) 函数获取一个值并截断小数点后 3 位数字。
  3. 我们取原始和,截断值后得到的和,然后计算差值。
  4. 然后我们将在同一组值上使用 round() 并找到原始总和,四舍五入后的总和以及两者之间的差值。

输出显示两种策略之间存在显着差异。当我们使用 truncate 而不是 round() 时,会损失很多精度,这可能会导致大量问题。

围捕

以十进制形式将数字四舍五入意味着将它们转换为大于该数字的整数。

它降低了您工作的精度,但也使计算更简单。在 Python 中,有两种方法可以对数字进行四舍五入:

使用 round() 进行四舍五入

在 Python 中对数字进行舍入的第一种方法是内置的舍入函数——round(n, i)。它以两个值作为参数;要四舍五入的数字“n”和“i”,数字需要四舍五入的小数位数。

第二个参数是可选的。如果你想对数字进行四舍五入,你只需要在 round() 中包含第一个参数,即你想要执行函数的值。

对于大于 0.5 的小数,round() 总是返回较大的整数值。

例如,如果您使用 round() 对 10.6 进行四舍五入,Python 解释器将显示 11。为了更清楚,让我们看一下下面的代码。

value1 = 6.925
value2 = 5.42
value3 = 9.87
round1 = round(value1)
round2 = round(value2)
round3 = round(value3)
print('Values'.ljust(20), 'Rounded Up')
print(str(value1).ljust(20), round1)
print(str(value2).ljust(20), round2)
print(str(value3).ljust(20), round3)

首先,我们将浮点值赋给三个变量 value1、value2 和 value3。接下来,我们将它们作为参数传递给 round() 函数。

最后,我们打印两列,一列为原始值,另一列为四舍五入后的值。您会注意到代码返回 7 和 10,即 6.925 和 9.87 的较高整数值。

但是,对于 5.42,输出为 5,因为小数位有 0.42 – 小于 0.6。这可能是许多领域的问题,这就是为什么我们继续使用第二种舍入数字的方法。

使用 math.ceil() 进行四舍五入

要使用 ceil() 函数,您必须导入数学库。此函数自动返回十进制数的较高整数值。

例如,无论小数点是否大于 0.5,10.2 和 10.6 都将返回 11。请参阅下面的代码:

import math
print(math.ceil(10.2))
print(math.ceil(10.6))

ceil() 的工作使得它比 round() 更方便地舍入数字。

向下舍入

如果您已经学会了如何四舍五入,那么四舍五入将是一件轻而易举的事。您可以通过两种方式解决它:

使用 round() 向下舍入

对于小数 0.5 及更小,round() 函数返回较小的整数值。对于 4.2,您将得到 4。让我们更改之前使用的代码以使事情更清楚。

value1 = 3.2
value2 = 4.5
value3 = 4.852
round1 = round(value1)
round2 = round(value2)
round3 = round(value3)
print('Values'.ljust(20), 'Rounded Down')
print(str(value1).ljust(20), round1)
print(str(value2).ljust(20), round2)
print(str(value3).ljust(20), round3)

首先,我们将浮点值存储在三个变量中。接下来,我们为原始值和舍入值打印两列。如您所见,我们得到了 3.2 和 4.5 的正确结果。

但是,对于像 4.852 这样的十进制数,round() 可能是错误的。另一件值得注意的事情是,如果小数位有 0.5,round() 将向上或向下舍入该值,具体取决于它位于其间的两个整数中的哪个是偶数。

因此,对于 0.5,该函数始终返回偶数。让我们看一下更方便的四舍五入方法。

使用 math.floor() 向下舍入

通过将数学库导入您的代码,您可以使用它的 floor() 函数。它会自动返回小数的较低值。

因此,对于 8.3 和 8.9,你得到 8。让我们考虑下面的代码:

import math
print(math.floor(8.3))
print(math.floor(8.9))

您可以看到代码的输出为我们提供了我们想要的确切结果,使 floor() 成为舍入所需数字的更好选择。

向上或向下舍入一半

四舍五入和四舍五入都是指将指定数字四舍五入到最接近的数字以保证更精确。

然而,在半向上,位于两个数字之间的值将转换为更高的数字。同时,half down 将值转换为较低的数字。

下面,我们整理了一段代码,看看上下半圆是如何工作的:

import decimal
import math
def roundHalfUp(value, decimals = 0):
    multiplyWith = 10 ** decimals
    return math.floor(value * multiplyWith + 0.5) / multiplyWith
def roundHalfDown(value, decimals = 0):
    multiplyWith = 10 ** decimals
    return math.ceil(value * multiplyWith - 0.5) / multiplyWith
print(roundHalfUp(15.28, 1))
print(roundHalfUp(15.25, 1))
print(roundHalfDown (1.5))
print(roundHalfDown(2.5, 1))

首先,将小数点向右移动到指定的位置。接下来,确定移动后的小数点后数字的值。

是大于 5、小于 5 还是等于 5?下一步是添加 0.5 并应用 floor 函数进行四舍五入。对于下半部分,您从数字中减去 0.5 并应用 math.ceil() 函数。

输出显示准确四舍五入的上下值。

舍入浮点数

现在您已经熟悉 round() 函数,您可以轻松地使用它来舍入浮点值。以前,我们省略了第二个用于向上和向下舍入的可选参数。

您所要做的就是指定要将数字四舍五入以舍入浮点值的位数。让我们看看下面的代码:

print(round(10.9258, 2))
print(round(6.9054, 1))
print(round(110.9873, 3))
print(round(2000.32537453, 4))
print(round(7.9424, 2))

输出显示每个浮点值都已四舍五入到指定为参数的小数位数。

舍入小数

Python 提供了一个 decimal 模块,可以在处理数字时提高精度。它是通过考虑人们在现实生活中如何进行算术而设计的。

因此,它保留有效数字,提供更精确的小数表示,并根据用户的方便提供灵活的精度。该模块提供了许多舍入策略:

  1. ROUND_CEILING:四舍五入值大于数字
  2. ROUND_DOWN:四舍五入的值向 0 移动
  3. ROUND_FLOOR:四舍五入值小于数字
  4. ROUND_HALF_DOWN:四舍五入的值向 0 移动一半
  5. ROUND_HALF_EVEN:四舍五入的值向最接近的偶数移动一半
  6. ROUND_HALF_UP:四舍五入的值从 0 移动一半
  7. ROUND_UP:四舍五入的值远离 0

让我们看下面的代码来观察这些函数是如何工作的:

import random
randomValues = [random.uniform(0.01, 0.1) for i in range(500000)]
def truncateNumber(num):
    return int(num * 1000) / 1000
sumOfNumbers = 0
sumOfTruncate = 0
for n in randomValues:
    sumOfNumbers = sumOfNumbers + n
    sumOfTruncate = truncateNumber(sumOfTruncate + n)
print("Truncating upto 3 decimal places:")
print("Original Sum = ", sumOfNumbers)
print("Total Using Truncate = ", sumOfTruncate)
print("Difference (truncate) = ", sumOfNumbers - sumOfTruncate, '\n')
print("Using round() upto 3 decimal places:")
sumOfNumbers1 = 0
sumOfTruncate1 = 0
for n in randomValues:
    sumOfNumbers1 = sumOfNumbers1 + n       
    sumOfTruncate1 = round(sumOfTruncate1 + n, 3)
print("Original Sum = ", sumOfNumbers1)
print("Total Using round() = ", sumOfTruncate1)
print("Difference (round()) = ", sumOfNumbers1 - sumOfTruncate1)

首先,我们导入 decimal 模块。接下来,我们给变量round_num赋一个float值,然后四舍五入到2位存入final_val。

最后,我们应用所有策略来四舍五入小数。在这里,quantize() 起着至关重要的作用。首先,在点 (.) 运算符之前,我们指定要四舍五入的值。

由于我们要对小数进行四舍五入,因此我们将 round_num 实例化为小数。在 .quantize() 的括号中,我们指定要舍入小数点的位置。

最后,我们打印了结果。输出显示结果中的差异,具体取决于所使用的舍入策略。

四舍五入到最接近的整数(5 或 10)

在某些情况下,您可能希望将值四舍五入为特定数字的倍数。让我们在这里考虑两个基数,5 和 10。

我们希望对一个数字进行四舍五入以获得始终是任一基数(即 5 或 10)的倍数的输出。例如,对于 14,四舍五入到最接近的整数 5 应该得到 15,而对于 10,你需要10个。

让我们看看下面的代码来了解如何做到这一点:

def myroundto5(n, base=5):
    return base * round(n/base)
def myroundto10(n, base=10):
    return base * round(n/base)
print(myroundto5(11))
print(myroundto5(14))
print(myroundto10(14))
print(myroundto10(21))

要四舍五入的值为 n,而底数为 5 或 10。底数可以设置为您希望的任何数字。下一步是将值除以底数并将结果四舍五入以获得整数。

最后,将这个整数乘以你的底数,得到它的倍数作为你的四舍五入的数字。输出显示不同的数字如何以 5 或 10 作为底数四舍五入。

四舍五入到最接近的分数

Python 还包括一个分数模块。它允许对分数进行大量算术计算或将分数四舍五入到所需的数字。让我们看一下下面的代码:

from fractions import Fraction
fractionValue = Fraction(50, 3)
roundedWithoutLimit = round(fractionValue)
roundedWithLimit = round(fractionValue, 2)
print('Fraction'.ljust(15), 'Round Without Limit'.ljust(20), 'Round With Limit')
print(str(fractionValue).ljust(15), str(roundedWithoutLimit).ljust(20), roundedWithLimit)

确保从 fractions 模块导入 Fraction 以在代码中使用它。接下来,我们实例化一个分数对象,并将分子指定为 50,将分母指定为 3。

接下来,我们以两种方式进行舍入。我们没有指定要对分数进行四舍五入的位数,并将值赋给变量 roundedWithoutLimit。

接下来,我们将分数四舍五入到小数点后两位,并将值赋给 roundedWithLimit。最后,我们将结果打印在三列中,其中两个结果之间的差异很明显。

舍入负数

现在您已经了解了如何将正数四舍五入,处理负数将不是问题。您可以通过三种方式舍入负数:

第一种方法是 Python 中存在的基本内置函数,即 round()。它同样适用于正整数和负整数。

如果小数位大于 0.5,则函数返回该值介于两个整数之间的较大整数。

另一方面,如果它小于 0.5,则 round() 返回较小的整数。请记住,对于 0.5,round() 始终返回偶数。

因此,对于两个浮点值 9.2 和 4.5,您分别得到 9 和 4。第二种方法是使用 math.ceil() 函数。Ceil 始终用于向上舍入值。

浮点值 -3.6 介于 -3 和 -4 之间。较大的整数是 -3,因此 ceil 返回它。因此,对于负值,ceil 向下移动,而不是它对正数起作用。

第三,您可以使用 math.floor() 函数。它始终用于向下舍入值。让我们以浮点值 -3 为例。它位于 -3 和 -4 之间。

较小的整数是 -4,因此 math.floor() 将返回它。因此,对于负值,它在数字尺度上向上移动,而不是它对正数起作用。

让我们看一下下面的代码,看看 Python 是如何对负数进行舍入的:

import math
value1 = -9.2
value2 = -4.5
value3 = -3.6
print('value1'.ljust(15), 'round()'.ljust(15), 'math.ceil()'.ljust(15), 'math.floor()')
print(str(value1).ljust(15), str(round(value1)).ljust(15), 
str(math.ceil(value1)).ljust(15), str(math.floor(value1)))
print(str(value2).ljust(15), str(round(value2)).ljust(15), 
str(math.ceil(value2)).ljust(15), str(math.floor(value2)))
print(str(value3).ljust(15), str(round(value3)).ljust(15), 
str(math.ceil(value3)).ljust(15), str(math.floor(value3)))

首先,我们导入数学库以确保可以访问 ceil 和 floor 函数。接下来,我们将浮点值分配给三个变量——value1、value2 和 value3。

最后,我们打印 4 列。第一个是负数,正在四舍五入。其他三列分别用于 round()、math.ceil()、math.floor()。

结果显示上述舍入负数的策略之间存在明显差异。

不带 round() 的圆形(使用 divmod)

divmod(num1, num2) 是 Python 中的一个函数,它将被除数和除数作为参数并返回商和余数的元组。我们可以利用它的工作原理将一个数字四舍五入到最接近的 5 或 10。让我们看一下下面的代码:

def roundUsingDivmod(value):
    quotient, remainder = divmod(value, 5)
    if remainder >= 5:
        quotient += 1
    return quotient * 5
print(roundUsingDivmod(105.2))

首先,我们定义了一个函数,它将要四舍五入的数字(即值)作为参数。接下来,我们对除数为 5 的值应用 divmod()。

要舍入到最接近的 10,请将第二个参数替换为 10。接下来,我们检查余数是否大于或等于 5。如果是,我们在商中加 1,这意味着我们正在四舍五入。

最后,我们将商乘以 5 以返回 5 的倍数作为四舍五入的答案。最后,我们将 105.2 作为该函数的参数传递。结果打印 105.0 作为答案,最接近 5 的倍数。

圆形 NumPy 数组

NumPy 数组广泛用于存储数据科学和科学计算中的数据。由于其矢量化和广播特性,它是一种实用的策略。

这些允许函数一次应用于整个数组,而不是一次应用于一个元素。确保在系统上安装了 NumPy 以使用 NumPy 数组。

这些数组可以使用 round() 和 around() 函数进行舍入。让我们看一下下面的代码:

import numpy as np
npArray = [0.323859, 3.302424, -7.3480239, 8.93483290, -0.3423423, 0.3423532]
round1 = np.round(npArray, 2)
round2 = np.around(npArray, 2)
print(round1, '\n', round2)

首先,我们导入 numpy 模块并将其分配给 np. 接下来,我们初始化一个 1×6 数组。最后,我们将使用 round() 和 around() 函数并传递数组和我们希望将该函数舍入到的位数。

最后,我们打印结果,您可以观察到这两个函数给出相同的结果。

文章原文出处:https: //likegeeks.com/

#python #round #numbers 

What is GEEK

Buddha Community

Python 中的整数
Shardul Bhatt

Shardul Bhatt

1626775355

Why use Python for Software Development

No programming language is pretty much as diverse as Python. It enables building cutting edge applications effortlessly. Developers are as yet investigating the full capability of end-to-end Python development services in various areas. 

By areas, we mean FinTech, HealthTech, InsureTech, Cybersecurity, and that's just the beginning. These are New Economy areas, and Python has the ability to serve every one of them. The vast majority of them require massive computational abilities. Python's code is dynamic and powerful - equipped for taking care of the heavy traffic and substantial algorithmic capacities. 

Programming advancement is multidimensional today. Endeavor programming requires an intelligent application with AI and ML capacities. Shopper based applications require information examination to convey a superior client experience. Netflix, Trello, and Amazon are genuine instances of such applications. Python assists with building them effortlessly. 

5 Reasons to Utilize Python for Programming Web Apps 

Python can do such numerous things that developers can't discover enough reasons to admire it. Python application development isn't restricted to web and enterprise applications. It is exceptionally adaptable and superb for a wide range of uses.

Robust frameworks 

Python is known for its tools and frameworks. There's a structure for everything. Django is helpful for building web applications, venture applications, logical applications, and mathematical processing. Flask is another web improvement framework with no conditions. 

Web2Py, CherryPy, and Falcon offer incredible capabilities to customize Python development services. A large portion of them are open-source frameworks that allow quick turn of events. 

Simple to read and compose 

Python has an improved sentence structure - one that is like the English language. New engineers for Python can undoubtedly understand where they stand in the development process. The simplicity of composing allows quick application building. 

The motivation behind building Python, as said by its maker Guido Van Rossum, was to empower even beginner engineers to comprehend the programming language. The simple coding likewise permits developers to roll out speedy improvements without getting confused by pointless subtleties. 

Utilized by the best 

Alright - Python isn't simply one more programming language. It should have something, which is the reason the business giants use it. Furthermore, that too for different purposes. Developers at Google use Python to assemble framework organization systems, parallel information pusher, code audit, testing and QA, and substantially more. Netflix utilizes Python web development services for its recommendation algorithm and media player. 

Massive community support 

Python has a steadily developing community that offers enormous help. From amateurs to specialists, there's everybody. There are a lot of instructional exercises, documentation, and guides accessible for Python web development solutions. 

Today, numerous universities start with Python, adding to the quantity of individuals in the community. Frequently, Python designers team up on various tasks and help each other with algorithmic, utilitarian, and application critical thinking. 

Progressive applications 

Python is the greatest supporter of data science, Machine Learning, and Artificial Intelligence at any enterprise software development company. Its utilization cases in cutting edge applications are the most compelling motivation for its prosperity. Python is the second most well known tool after R for data analytics.

The simplicity of getting sorted out, overseeing, and visualizing information through unique libraries makes it ideal for data based applications. TensorFlow for neural networks and OpenCV for computer vision are two of Python's most well known use cases for Machine learning applications.

Summary

Thinking about the advances in programming and innovation, Python is a YES for an assorted scope of utilizations. Game development, web application development services, GUI advancement, ML and AI improvement, Enterprise and customer applications - every one of them uses Python to its full potential. 

The disadvantages of Python web improvement arrangements are regularly disregarded by developers and organizations because of the advantages it gives. They focus on quality over speed and performance over blunders. That is the reason it's a good idea to utilize Python for building the applications of the future.

#python development services #python development company #python app development #python development #python in web development #python software development

Art  Lind

Art Lind

1602968400

Python Tricks Every Developer Should Know

Python is awesome, it’s one of the easiest languages with simple and intuitive syntax but wait, have you ever thought that there might ways to write your python code simpler?

In this tutorial, you’re going to learn a variety of Python tricks that you can use to write your Python code in a more readable and efficient way like a pro.

Let’s get started

Swapping value in Python

Instead of creating a temporary variable to hold the value of the one while swapping, you can do this instead

>>> FirstName = "kalebu"
>>> LastName = "Jordan"
>>> FirstName, LastName = LastName, FirstName 
>>> print(FirstName, LastName)
('Jordan', 'kalebu')

#python #python-programming #python3 #python-tutorials #learn-python #python-tips #python-skills #python-development

Art  Lind

Art Lind

1602666000

How to Remove all Duplicate Files on your Drive via Python

Today you’re going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates.

Intro

In many situations you may find yourself having duplicates files on your disk and but when it comes to tracking and checking them manually it can tedious.

Heres a solution

Instead of tracking throughout your disk to see if there is a duplicate, you can automate the process using coding, by writing a program to recursively track through the disk and remove all the found duplicates and that’s what this article is about.

But How do we do it?

If we were to read the whole file and then compare it to the rest of the files recursively through the given directory it will take a very long time, then how do we do it?

The answer is hashing, with hashing can generate a given string of letters and numbers which act as the identity of a given file and if we find any other file with the same identity we gonna delete it.

There’s a variety of hashing algorithms out there such as

  • md5
  • sha1
  • sha224, sha256, sha384 and sha512

#python-programming #python-tutorials #learn-python #python-project #python3 #python #python-skills #python-tips

How To Compare Tesla and Ford Company By Using Magic Methods in Python

Magic Methods are the special methods which gives us the ability to access built in syntactical features such as ‘<’, ‘>’, ‘==’, ‘+’ etc…

You must have worked with such methods without knowing them to be as magic methods. Magic methods can be identified with their names which start with __ and ends with __ like init, call, str etc. These methods are also called Dunder Methods, because of their name starting and ending with Double Underscore (Dunder).

Now there are a number of such special methods, which you might have come across too, in Python. We will just be taking an example of a few of them to understand how they work and how we can use them.

1. init

class AnyClass:
    def __init__():
        print("Init called on its own")
obj = AnyClass()

The first example is _init, _and as the name suggests, it is used for initializing objects. Init method is called on its own, ie. whenever an object is created for the class, the init method is called on its own.

The output of the above code will be given below. Note how we did not call the init method and it got invoked as we created an object for class AnyClass.

Init called on its own

2. add

Let’s move to some other example, add gives us the ability to access the built in syntax feature of the character +. Let’s see how,

class AnyClass:
    def __init__(self, var):
        self.some_var = var
    def __add__(self, other_obj):
        print("Calling the add method")
        return self.some_var + other_obj.some_var
obj1 = AnyClass(5)
obj2 = AnyClass(6)
obj1 + obj2

#python3 #python #python-programming #python-web-development #python-tutorials #python-top-story #python-tips #learn-python

Arvel  Parker

Arvel Parker

1593156510

Basic Data Types in Python | Python Web Development For Beginners

At the end of 2019, Python is one of the fastest-growing programming languages. More than 10% of developers have opted for Python development.

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Table of Contents  hide

I Mutable objects

II Immutable objects

III Built-in data types in Python

Mutable objects

The Size and declared value and its sequence of the object can able to be modified called mutable objects.

Mutable Data Types are list, dict, set, byte array

Immutable objects

The Size and declared value and its sequence of the object can able to be modified.

Immutable data types are int, float, complex, String, tuples, bytes, and frozen sets.

id() and type() is used to know the Identity and data type of the object

a**=25+**85j

type**(a)**

output**:<class’complex’>**

b**={1:10,2:“Pinky”****}**

id**(b)**

output**:**238989244168

Built-in data types in Python

a**=str(“Hello python world”)****#str**

b**=int(18)****#int**

c**=float(20482.5)****#float**

d**=complex(5+85j)****#complex**

e**=list((“python”,“fast”,“growing”,“in”,2018))****#list**

f**=tuple((“python”,“easy”,“learning”))****#tuple**

g**=range(10)****#range**

h**=dict(name=“Vidu”,age=36)****#dict**

i**=set((“python”,“fast”,“growing”,“in”,2018))****#set**

j**=frozenset((“python”,“fast”,“growing”,“in”,2018))****#frozenset**

k**=bool(18)****#bool**

l**=bytes(8)****#bytes**

m**=bytearray(8)****#bytearray**

n**=memoryview(bytes(18))****#memoryview**

Numbers (int,Float,Complex)

Numbers are stored in numeric Types. when a number is assigned to a variable, Python creates Number objects.

#signed interger

age**=**18

print**(age)**

Output**:**18

Python supports 3 types of numeric data.

int (signed integers like 20, 2, 225, etc.)

float (float is used to store floating-point numbers like 9.8, 3.1444, 89.52, etc.)

complex (complex numbers like 8.94j, 4.0 + 7.3j, etc.)

A complex number contains an ordered pair, i.e., a + ib where a and b denote the real and imaginary parts respectively).

String

The string can be represented as the sequence of characters in the quotation marks. In python, to define strings we can use single, double, or triple quotes.

# String Handling

‘Hello Python’

#single (') Quoted String

“Hello Python”

# Double (") Quoted String

“”“Hello Python”“”

‘’‘Hello Python’‘’

# triple (‘’') (“”") Quoted String

In python, string handling is a straightforward task, and python provides various built-in functions and operators for representing strings.

The operator “+” is used to concatenate strings and “*” is used to repeat the string.

“Hello”+“python”

output**:****‘Hello python’**

"python "*****2

'Output : Python python ’

#python web development #data types in python #list of all python data types #python data types #python datatypes #python types #python variable type