栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Create Number, String, List variables

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Create Number, String, List variables

We can create different types of variables as per our requirements. Let's see each one by one.

Number

A number is a data type to store numeric values. The object for the number will be created when we assign a value to the variable. In Python3, we can use the following three data types to store numeric values.

#1 int

#2 float

#3 complex

Integer variable

The int is a data type that returns integer type values (signed integers). They are also called ints or integers. The integer value can be positive or negetive without a decimal point.

Example:

# create integer variable
age = 25
print(age) # 25
print(type(age)) # 

Note: We used the built-in Python method type() to check the variable type.

Float variable

Floats are the values with the decimal point dividing the integer and the fractional parts of the number. User float data type to store decimal values.

Example:

# create float variable
salary = 3000.0
print(salary) # 3000.0
print(type(salary)) # 

In the above example, the variable salary assigned to value 3000.0, which is a float value.

Float variable

The complex is the numbers that come with the real and imaginary part. A complex number is in the form of  a + bj, where a and b contain integers or floating-point values.

Example:

a = 3 + 5j
print(a) # (3+5j)
print(type(a)) # 

String variable

In Python, a string is a set of characters represented in quotation marks. Python allows us to define a string in either pair of single or double quotation marks. For example, to store a person's name we can use a string type.

To retrieve a piece of string from a given string, we can use to slice operator [ ] or [ : ]. Slicing provides us the subset of a string with an index starting from index 0 to index end -1.

To concatenate the string, we can use the addition (+) operator.

Example:

# create a variable of type string
str = 'python'
# prints complete string
print(str) # python

# prints first character of the string
print(str[0]) # p

# prints characters starting from 2nd to 5th
print(str[2:5]) # tho

# length of string
print(len(str)) # 6

# concatenate string
print(str + "Test") # pythonTest

List type variable

If we want to represent a group of elements (or value) as a single entity, we should go for the list variable type. For example, we can use them to store student names. In the list, the insertion order of elements is preserved. That means, in which order elements are inserted in the list, the order will be intact.

The list can be accessed in two ways, either positive or negative index. The list has the following characteristics:

1. In the list insertion order of elements is preserved.

2. Heterogeneous (all types of data types int, float, string) elements are allowed.

3. Duplicates elements are permitted.

4. The list is mutable(can change).

5. Growable in nature means based on our requirement, we can increase or decrease the list's size.

6. List elements should be enclosed within square brackets [ ].

Example:

# create list
ls = ['John', 10, 20, 'Tom', 50, 10.5]
# print entire list
print(ls) # ['John', 10, 20, 'Tom', 50, 10.5]

# accessing 1st element of a list
print(ls[0]) # 'John'

# accessing last element of a list
print(ls[-1]) # 10.5

# access chunks of list data
print(ls[1:3]) # [10,20]

# modifying first element of a list
ls[0] = 'Jenny'
print(ls[0]) # 'Jenny'

# add one more elements to list
ls.append(100)
print(ls) # ['Jenny', 10, 20, 'Tom', 50, 10.5, 100]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878891.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号