>>> x = 100
>>> y = 10
>>> x < y and x or Y
10
>>> x if x > y else y
100
If statement:
>>> x = 10
>>> if x = = 10:
... print (x)
...
10
>>> if x = = 10:
... print (x)
.. else:
... print ("X not 10!")
...
10
>>> if x = = 10:
... print (x)
... elif x = = 100:
... print (x)
.. else:
... print (x)
...
10
While loop:
[Email protected]:/python$ cat w.py
#!/usr/bin/env python
LIST1=[1,2,3,4,5,6,7,8,9,10]
sum = 0
While List1:
Sum + = List1[0]
List1 = list1[1:]
Print (sum)
[Email protected]:/python$ python w.py
55
For loop:
[Email protected]:/python$ cat f.py
#!/usr/bin/env python
List1 = [1,3,5,6,9]
For I in List1:
Print (i)
[Email protected]:/python$ python f.py
1
3
5
6
9
Else statement:
[Email protected]:/python$ cat f.py
#!/usr/bin/env python
List1 = [1,3,5,6,9]
For I in List1:
Print (i)
Else
Print ("While is normal over!")
[Email protected]:/python$ python f.py
1
3
5
6
9
While is normal over!
Break statement:
[Email protected]:/python$ cat f.py
#!/usr/bin/env python
List1 = [1,3,5,6,9]
For I in List1:
Print (i)
Break
Else
Print ("While is normal over!")
[Email protected]:/python$ python f.py
1
Continue statement:
[Email protected]:/python$ cat f.py
#!/usr/bin/env python
List1 = [1,3,5,6,9]
For I in List1:
if i = = 5:
Continue
Print (i)
Else
Print ("While is normal over!")
[Email protected]:/python$ python f.py
1
3
6
9
While is normal over!
Loops in Python