Home Backend Development Python Tutorial Python bits and pieces

Python bits and pieces

Nov 26, 2016 am 10:15 AM

# -*- coding: UTF-8 -*-
import sys;
x = 'runoob'; sys.stdout.write(x + '\n')
print("hello world!");
#print("中国,会不会乱码!"); utf-8之后,还乱码
if False:
print("True");
else:
print("False");
#var
name = "zhangsan"
age = 23
print(name);
print(age);
del name  #删除对象的引用
del age
print("-------------string--------------")
#string
str = 'Hello World!'
print(str) # 输出完整字符串
print(str[0]) # 输出字符串中的第一个字符
print(str[2:5]) # 输出字符串中第三个至第五个之间的字符串
print(str[2:]) # 输出从第三个字符开始的字符串
print(str * 2) # 输出字符串两次
print(str + "TEST") #输出连接的字符串
print("-------------list--------------")
#list
list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
tinylist = [123, 'john']
print(list) # 输出完整列表
print(list[0]) # 输出列表的第一个元素
print(list[1:3]) # 输出第二个至第三个的元素 
print(list[2:]) # 输出从第三个开始至列表末尾的所有元素
print(tinylist * 2) # 输出列表两次
print(list + tinylist) # 打印组合的列表
print("-------------元组--------------")
print("元组用括号标识。内部元素用逗号隔开。但是元组不能二次赋值,相当于只读列表。");
tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )
tinytuple = (123, 'john')
print(tuple) # 输出完整元组
print(tuple[0]) # 输出元组的第一个元素
print(tuple[1:3]) # 输出第二个至第三个的元素 
print(tuple[2:]) # 输出从第三个开始至列表末尾的所有元素
print(tinytuple * 2) # 输出元组两次
print(tuple + tinytuple) # 打印组合的元组
print("-------------元字典 (类似于java中的map)--------------")
dict = {}
dict['one'] = "This is one"
dict[2] = "This is two"
tinydict = {'name': 'john','code':6734, 'dept': 'sales'};
print(dict['one']); # 输出键为'one' 的值
print(dict[2]); # 输出键为 2 的值
print(dict); 
print(tinydict) # 输出完整的字典
print(tinydict.keys()) # 输出所有键
print(tinydict.values()) # 输出所有值
print("-------------数据类型转换--------------")
print("数据类型转化在如下的url中");
print("http://www.runoob.com/python/python-variable-types.html");
print("-------------运算符--------------")
a = 21
b = 10
c = 0
c = a + b
print("c 的值为:", c)
if ( a != b ):
   print("3 - a 不等于 b");
else:
   print("3 - a 等于 b");
c = a | b;        # 61 = 0011 1101 
print("2 - c 的值为:", c);
a = 4
b = 20
list = [1, 2, 3, 4, 5 ];
#成员运算符
if ( a in list ):
   print("1 - 变量 a 在给定的列表中 list 中");
else:
   print("1 - 变量 a 不在给定的列表中 list 中");
if ( b not in list ):
   print("2 - 变量 b 不在给定的列表中 list 中");
else:
   print("2 - 变量 b 在给定的列表中 list 中");
   
   
#身份运算符
a = 20;
b = 20;
c = 100;
if ( a is b ):
   print("1 - a 和 b 有相同的标识");
else:
   print("1 - a 和 b 没有相同的标识");
if ( a is not c ):
   print("1 - a 和 c 有相同的标识");
else:
   print("1 - a 和 c 没有相同的标识");
    
if ( id(a) == id(b) ):
   print("2 - a 和 b 有相同的标识");
else:
   print("2 - a 和 b 没有相同的标识");
   
   
   
   
#循环   [break和else两者的区别]
#在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。
i = 1
while 1:            # 循环条件为1必定成立
    print(i);         # 输出1~10
    i += 1;
    if i > 10:     # 当i大于10时跳出循环
        break;
   
   
   
count = 0
while count < 5:
   print(count, " is  less than 5");
   count = count + 1;
else:
   print(count, " is not less than 5");
   
   
for num in range(10,20):  # 迭代 10 到 20 之间的数字
   for i in range(2,num): # 根据因子迭代
      if num%i == 0:      # 确定第一个因子
         j=num/i          # 计算第二个因子
         print(&#39;%d 等于 %d * %d&#39; % (num,i,j));
         break;            # 跳出当前循环
   else:                  # 循环的 else 部分
      print(num, &#39;是一个质数&#39;);
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

What are some popular Python libraries and their uses? What are some popular Python libraries and their uses? Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

What are regular expressions? What are regular expressions? Mar 20, 2025 pm 06:25 PM

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

See all articles