02 Python Programming Basics Part I
02 Python Programming Basics Part I
Basics
By:
Sanka Sandamal
Prerequisites
• You should have a basic understanding of Computer Programming
terminologies. A basic understanding of any of the programming
languages is a plus.
1. Class names start with an uppercase letter. All other identifiers start
with a lowercase letter.
2. There are some reserved words which you cannot use as a variable
name because Python uses them for other things.
• Here, "\n\n" is used to create two new lines before displaying the actual
line. Once the user presses the key, the program ends. This is a nice trick
to keep a console window open until the user is done with an application.
if x > 100 :
print('Excellent')
elif x > 50 :
print('Good')
elif x > 30 :
print('Must improve')
else :
print('Fail')
X = 50
y = ‘100’
text = “Hello world”
a=b=c=1
a, b, c = 1, 2, "john"
• Escape Characters
Following table is a list of escape or non-printable characters that can be
represented with backslash notation.An escape character gets interpreted;
in a single quoted as well as double quoted strings.
\b , \e , \n , \s , \t
“+”
Concatenation - Adds values on either side of the operator
“*”
Repetition - Creates new strings, concatenating multiple copies of the
same string
“[ ]”
Slice - Gives the character from the given index
“[ : ]”
Range Slice - Gives the characters from the given range
“not in”
Membership - Returns true if a character does not exist in the given string
%c – character
%s - string conversion via str() prior to formatting
%i , %d - signed decimal integer
%f - floating point real number
String center()
The method center() returns centered in a string of length width. Padding
is done using the specified fillchar. Default filler is a space.
str = "this is string example....wow!!!“
print ("str.center(40, 'a') : ", str.center(40, 'a'))
Result : aaaathis is string example....wow!!!aaaa
• The istitle() method checks whether all the case-based characters in the
string following non-casebased letters are uppercase and all other case-
based characters are lowercase.
• The method lower() returns a copy of the string in which all case-based
characters have been lowercased.
• The min() method returns the min alphabetical character from the string
str.
• The swapcase() method returns a copy of the string in which all the case-
based characters have had their case swapped.
• To access values in lists, use the square brackets for slicing along with the
index or indices to obtain value available at that index.
• To remove a list element, you can use either the del statement if you know
exactly which element(s) you are deleting.
Lists respond to the + and * operators much like strings; they mean
concatenation and repetition here too, except that the result is a new list,
not a string.
• Tuples are immutable, which means you cannot update or change the
values of tuple elements. You are able to take portions of the existing
tuples to create new tuples as the following example demonstrates –
“+” Addition
Adds values on either side of the operator.
“-” Subtraction
Subtracts right hand operand from left hand operand.
“*” Multiplication
Multiplies values on either side of the operator
“/” Division
Divides left hand operand by right hand operand
“%” Modulus
Divides left hand operand by right hand operand and returns remainder
“==”
If the values of two operands are equal, then the condition becomes true.
“!=”
If values of two operands are not equal, then condition becomes true.
“>”
If the value of left operand is greater than the value of right operand, then
condition becomes true.
“<”
If the value of left operand is less than the value of right operand, then
condition becomes true.
“<=”
If the value of left operand is less than or equal to the value of right
operand, then condition becomes true.
“=”
Assigns values from right side operands to left side operand
“+=”
It adds right operand to the left operand and assign the result to left
operand
“-=”
It subtracts right operand from the left operand and assign the result to
left operand
“*=”
It multiplies right operand with the left operand and assign the result to
left operand
“%=”
It takes modulus using two operands and assign the result to left operand
“**=”
Performs exponential (power) calculation on operators and assign value to
the left operand
“//=”
It performs floor division on operators and assign value to the left operand
“| Binary OR”
It copies a bit, if it exists in either operand.
“^ Binary XOR”
It copies the bit, if it is set in one operand but not both.
“in”
Evaluates to true if it finds a variable in the specified sequence and false
otherwise.
“not in”
Evaluates to true if it does not finds a variable in the specified sequence
and false otherwise.
“is”
Evaluates to true if the variables on either side of the operator point to the
same object and false otherwise.
“is not”
Evaluates to false if the variables on either side of the operator point to
the same object and true otherwise.
Thank You.
By:
Sanka Sandamal