Showing posts with label python-3. Show all posts
Showing posts with label python-3. Show all posts

Wednesday, May 15, 2019

print(5 * '=' * 5 == '=' * 5 * 5 == 5 * 5 * '=')


- By Vasudev Ram - Online Python training / SQL training / Linux training


Hi readers,

I was reviewing some of my Python training material, and saw these lines:
# Multiplying a string by a number results in another string
# with that many copies of the original string.
print "-" * 10
print 10 * '-'

print "=" * 10
print 10 * '='
Notice that the integer can be either to the left or right of the asterisk, and the same for the string, in the expressions in the print statements above.

Thought of experimenting a bit more, and came up with these snippets:
print(5 * '=' == '=' * 5)
True
print(5 * '=' * 5 == '=' * 5 * 5 == 5 * 5 * '=')
True
Which I initially found a little surprising, but if you think about, if comparisons using the less-than or greater-than signs (or variations like less-than-or-equal and greater-than-or-equal) can be chained in Python, why can't comparisons using the equals sign be chained too?

This works in both Python 2 and Python 3.

- Enjoy.

- Vasudev Ram - Online Python training and consulting

I conduct online courses on Python programming, Unix / Linux commands and shell scripting and SQL programming and database design, with course material and personal coaching sessions.

The course details and testimonials are here.

Contact me for details of course content, terms and schedule.

Try FreshBooks: Create and send professional looking invoices in less than 30 seconds.

Sell your digital products via DPD: Digital Publishing for Ebooks and Downloads.

Learning Linux? Hit the ground running with my vi quickstart tutorial. I wrote it at the request of two Windows system administrator friends who were given additional charge of some Unix systems. They later told me that it helped them to quickly start using vi to edit text files on Unix. Of course, vi/vim is one of the most ubiquitous text editors around, and works on most other common operating systems and on some uncommon ones too, so the knowledge of how to use it will carry over to those systems too.

Check out WP Engine, powerful WordPress hosting.

Teachable: feature-packed course creation platform, with unlimited video, courses and students.

Posts about: Python * DLang * xtopdf

My ActiveState Code recipes

Follow me on:


Monday, August 8, 2016

How to kill yourself in Python

By Vasudev Ram

Here's a simple Python program that shows how the os.kill function from Python's standard library, along with the os.getpid function and the signal module [1], can be used to terminate the current program - the one it is called from:
'''
Program: test_self_kill.py
A program to show that the os.kill function 
can be used to terminate the current program.
Author: Vasudev Ram
Copyright 2016 Vasudev Ram
https://vasudevram.github.io
http://jugad2.blogspot.com
https://gumroad.com/vasudevram
'''

from __future__ import print_function
import sys, os, signal

print("Python version:", sys.version)
print("This line will be printed.")
os.kill(os.getpid(), signal.SIGTERM)
print("If os.kill works, this line will not be printed.")
Program output when run in Python 2.7:
$ python test_self_kill.py
Python version: 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500
 64 bit (AMD64)]
This line will be printed.
Program output when run in Python 3.6:
$ python test_self_kill.py
Python version: 3.6.0a2 (v3.6.0a2:378893423552, Jun 14 2016, 01:21:40) [MSC v.19
00 64 bit (AMD64)]
This line will be printed.
As you can see, the second call to the print function does not run, because the program terminates itself.
You can read about Unix signals here and here.

- Vasudev Ram - Online Python training and consulting



My Python posts     Subscribe to my blog by email

My ActiveState recipes