python - variables
Variables are:
- storage locations that have a name
- name-value pairs
such as,
fruit = 'apple'
fruit = 'orange'
Variables
- case sensitive (case does matters)
- Fruit and fruit are different variables
- Must start with a letter
- can contain numbers
- Underscores allowed in variable names
- Not allowed:
-> + (plu sign)
-> - (Hyphen)
myvalues1 = 'abc'
my_Value_1 = 'ABC'
My_value_One = 'AbC'
Strings
- Represent text
- surrounded by quotes (single or double)
fruit = 'apple'
fruit = "apple"
Remember agian, strings can be included inside single or double quotes..
Using quotes within strings
if you want to include singe quote in a string, inclose the string in double quotation marks.
string = "That's simply Awesome !!"
To include double quote in a string, place them within single quote.
string = 'She said, "Life is beautiful !!"'
What if you want to include single and double quote on a text
by using skip sequence
double = "She said, \"That's simply awesome!\""
echo ${double}
single = 'She said, "Thats\'s simply awesome !"'
echo ${single}
Indexing
String: a p p l e
Index: - 1 2 3 4
a = 'apple'[0]
e = 'apple'[4]
fruit = 'apple'
first_character = fruit[0]
comments
# this is a comment. Python does not read this line.
# you can write what you trying
# to perform
"""
You can do this way as well
and it will not be read
"""
for eg,
# get the input from user
userinput = input('What would you like to order sir?')
# Find out the length of the input
text_length = len(userinput)
# make the border same size as the input
print(' {}'.format('_' * text_length))
print(' < {} >'.format(text))
print(' {}'.format('-' * text_length))
exception haldling
Finding an inten in a list
animals = [ 'man', 'bear', 'pig' ]
bear_index = animals.index('bear')
print(bear_index)
returns 1, because the bear is index 1.
-------------------------
Exception
animals = [ 'man', 'bear', 'pig' ]
cat_index = animals.index('cat')
print(cat_index)
what happens in this case?
cat is not on the index. so, it will throw error.
exceptions mean somethig wrong or something unexpected happened.
when error is generated, it will show you line and the exception.
review it.
what to do when exception happens.
Exception handling
try except block
animals = [ 'man', 'bear', 'pig' ]
try:
cat_index = animals.index('cat')
except:
cat_index = 'No cat found in the index.'
print(cat_index)
if you run above script, you will see the value no cat found.
# python3 animal1.py
No cat found in the index.
[root@master shell]# cat animal1.py
animals = [ 'man', 'bear', 'pig' ]
try:
cat_index = animals.index('cat')
except:
cat_index = 'No cat found in the index.'
print(cat_index)
[root@master shell]#
Tuesday, February 2, 2021
Python - variables
Subscribe to:
Post Comments (Atom)
Git branch show detached HEAD
Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03) 00 2. Run re-set hard $ git reset --hard 3. ...
-
snmpconfig command allow you to managge snmpv1/v3 agent configuration on SAN switch. Event trap level is mapped with event severity level....
-
Firmware upgrade on HPE SuperDom Flex 280 - prerequisites tasks a. Set up repo b. Upload firmware to your webserver 1. For foundation so...
-
Disabling the Telnet protocol on Brocade SAN switches By default, telnet is enabled on Brocade SAN switches. As part of security hardening o...
No comments:
Post a Comment