Identifiers & Reserved words in Python

Identifiers

  • A Python identifier is a name used to identify a variable, function, class, module, or any other object.
  • Naming Rules

üVariable length can be of anything.

üIdentifier names should start with an alphabet or underscore(_) followed by zero or more letters, underscores and digits

üNo other special characters are allowed.

üIdentifier names are case sensitive.20

Reserved Words

False class finally is return

None continue for lambda try

True def from nonlocal while

and del global not with

as elif if or yield

assert else import pass

break except in raise 21

Comments

Comments in Python start with the hash character, #, and extend to the end of the physical line.

A comment may appear at the start of a line or following white space or code, but not within a string literal.

’’’ (triple quotes) serves as multi-line comment. It can be used to generate documentation automatically.

# this is the first comment

SPAM = 1 # and this is the second comment

# … and now a third!

STRING = “# This is not a comment.”

The print Statement

>>> print(‘hello‘)

hello

>>> print(‘hello’, ‘there‘)

hello there

  • The print statement prints its argument to the output stream. Elements separated by commas print with a space between them.
  • A formatted printing can be done as below

 

print(“Value of a=%d and b=%d “%(a,b))

 

Arithmetic Operators Comparison operators Logical Operators Assignment Operators Bit wise operators Membership and identity operators
+ > and  =  & In, not in, is or is not
< or +=  I
* >= Not -= ^
/ <= /= ~
% == *= >>
** != <<
//

Data Types

  • Python supports the below standard data types:
  • Numbers
  • String
  • Boolean
  • List
  • Tuple
  • Set
  • Dictionary

Python Numbers

  • Number data types store numeric values. They are immutable data types which mean that changing the value of a number data type results in a newly allocated object.

Number objects are created when one assigns a value to them.

 

>>> var1 = 1

>>> var2 = 10

To delete the reference to a number object using the del statement.

 

>>> del var

>>> del var_a, var_b

Python Numbers

  • int (Integers – No limit to the value of integers)

Octal Ex) 0o12

Hexadecimal Ex) 0xF

float (floating point values in scientific or exponential form)

complex (complex numbers)

Strings

  • Strings can be enclosed in single, double or triple quotes.
  • Usually the single quote for a word, double quote for a line and triple quote for a paragraph.

 

>>>word = ‘word’

>>>sentence = “This is a sentence.”

>>>paragraph = “““This is a \

paragraph \

across \

multiple lines.”””

  • Starting with Python 3.0 all strings support Unicode 3

 

Raw String

  • A “raw” string will not convert \n sequences to newlines. A raw string precedes with ‘r’

 

>>> print(‘C:\some\name’)

C:\some

ame

>>> print(r’C:\some\name’)

C:\some\name

Python Strings

>>>str = ‘Hello World!’

>>>str # Prints complete string

>>>str[0] # Prints first character of the string

>>>str[2:5] # Prints characters starting from 3rd to 5th

>>>str[2:] # Prints string starting from 3rd character

>>>str[-1] # Prints the last item from the end

>>>str * 2 # Prints string two times

>>>str +  “TEST” # Prints concatenated string

String Operations

>>>s =   “Python“

>>>len(s)   # Length of the string

6

>>>s.find(‘t’)   # Use “find” to find the start of a substring.

2

>>>s.replace  (‘P’, ‘J’) # Replace a substring with another

Jython

>>>s.upper()  # Change to upper case

PYTHON

>>>s= ‘aaa,bbb,ccc,dd’

>>>s.split (“,”) # Split the string into parts using ‘,’ as delimiter

[‘aaa’,’bbb’,’ccc’,’dd’]

>>>s.isalpha() # Content tests: isalpha, isdigit, etc.

 

 

 

If you have questions, please ask below

Leave a Reply

If you have any questions headover to our forums

You can use these XHTML tags: <a href="" title=""> <abbr title=""> <acronym title=""> <blockquote cite=""> <code> <em> <strong>