-
Open a terminal by pressing [CTRL]+[ALT]+[T] keys
-
Type
python3
and press [ENTER] -
Try out below mentioned examples, and understand what's happening.
-
To exit the interpreter, type
quit()
and press [ENTER]
+
,-
,*
,/
,//
(floor division),%
(remainder),**
(power),=
-
Run following two commands and explain the difference in output
>>> print('C:\some\name') >>> print(r'C:\some\name')
Which of the above represents a raw string?
-
String literals can span multiple lines by using triple-quotes:
"""..."""
or'''...'''
. -
Strings can be concatenated with the
+
operator, and repeated with*
>>> 3 * 'un' + 'ium'
-
Slicing
>>> word = 'Python' >>> word[0:2] # characters from position 0 (included) to 2 (excluded) >>> word[2:5] # characters from position 2 (included) to 5 (excluded) >>> word[:2] # character from the beginning to position 2 (excluded) >>> word[4:] # characters from position 4 (included) to the end >>> word[-2:] # characters from the second-last (included) to the end
Note how the start is always included, and the end always excluded. This makes sure that
s[:i]
+s[i:]
is always equal tos
:
>>> cubes = [1, 8, 27, 65, 125]
>>> cubes.append(216) # add the cube of 6
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters[2:5] = []
>>> letters[:] = []
>>> len(letters)
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print('Negative changed to zero')
... elif x == 0:
... print('Zero')
... elif x == 1:
... print('Single')
... else:
... print('More')
...
>>> # Measure some strings:
... words = ['cat', 'window', 'defenestrate']
>>> for w in words:
... print(w, len(w))
...
>>> # Fibonacci series:
... a, b = 0, 1
>>> while a < 1000:
... print(a, end=',')
... a, b = b, a+b
...
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,
>>> for i in range(5):
... print(i)
...
>>> list(range(0, 10, 3))
[0, 3, 6, 9]
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i in range(len(a)):
... print(i, a[i])
...
>>> for n in range(2, 10):
... for x in range(2, n):
... if n % x == 0:
... print(n, 'equals', x, '*', n//x)
... break
... else:
... # loop fell through without finding a factor
... print(n, 'is a prime number')
...
>>> for num in range(2, 10):
... if num % 2 == 0:
... print("Found an even number", num)
... continue
... print("Found an odd number", num)
...
>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n."""
... a, b = 0, 1
... while a < n:
... print(a, end=' ')
... a, b = b, a+b
... print()
...
>>> fib(2000)
def ask_ok(prompt, retries=4, reminder='Please try again!'):
while True:
ok = input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise ValueError('invalid user response')
print(reminder)
This function can be called in several ways:
-
By giving only the mandatory argument:
ask_ok('Do you really want to quit?')
-
By giving one of the optional arguments:
ask_ok('OK to overwrite the file?', 2)
-
By giving all arguments:
ask_ok('OK to overwrite the file?', 2, 'Come on, only yes or no!')