-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf-Strings.py
More file actions
65 lines (46 loc) · 915 Bytes
/
f-Strings.py
File metadata and controls
65 lines (46 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon May 10 00:19:58 2021
@author: maherme
"""
#%%
# Python 3.6 introduces f-Strings.
# f-Strings is short for formatted string literals.
from sys import version_info
print(version_info)
#%%
# Three ways to do the same:
'{} % {} = {}'.format(10, 3, 10 % 3)
'{1} % {2} = {0}'.format(10 % 3, 10, 3)
'{a} % {b} = {mod}'.format(a=10, mod=10 % 3, b=3)
#%%
# You can use f'':
a = 10
b = 3
f'{a} % {b} = {a % b}'
a = 10/3
f'{a:0.5f}'
f'{10/3:0.5f}'
name = 'Python'
f'{name} rocks!'
#%%
def outer():
name = 'Python'
def inner():
return f'{name} rocks!'
return inner
print(outer()())
#%%
sq = lambda x: x**2
a = 10
b = 1
print(f'{sq(a) if b > 5 else a}')
b = 10
print(f'{sq(a) if b > 5 else a}')
a = 10
b = 1
print(f'{(lambda x: x**2)(a) if b > 5 else a}')
b = 10
print(f'{(lambda x: x**2)(a) if b > 5 else a}')
#%%