python
10 October 2024 11:51
Indentation : space for writing code , pyhton will give error if you skip indentation ,
number of spaces up to programmer, we should use same number of spaces fro all
lines in same block of code, python uses indentation to indicate a block of code.
Comments in python : comments can be used in python to explain python code,
to make code readable, to prevent code from execution while testing.
Comments start with # . Types of comments : single line, multi line, docstring
comments.
Python variables
• Variables are like containers for storing data values
• Variables names are case-sensitive
• Python has no command for declaring a variable. Variable will get create the
moment you assign value to it.
• Variables can change type after they have been set
• We can get the type of variable using type() function.
Variable names
I. A variable name must start with a letter or the underscore character
II. A variable name cannot start with number
III. A variable name can only contain alpha- numeric characters and underscores (A-z,
0 - 9, and _ )
IV. Variable names are case sensitive (age, Age, AGE are three different variable.
Global variable & local variables in python
> Local variables
a. Local variables that are created inside a function
b. Local variables can be used inside function only .
> Global variable
1. Variables that are created outside function know as global variables.
2. Global variables can be used everywhere, both inside and outside function
3. If we create a variable with same name inside function, still both local and global
variable are different. Local variable can be used only with in the function.
> Global keyword
• If you would like to create global variable inside function, we should use "global"
keyword
• To change the value of global variable inside function, use "global" keyword.
Data types in python
• In programming language data types are important
subject
• Variables can store data of different data types
• In python, the moment you assign value to
variable, that’s when data type will be set for
variable.
PYTHON Page 1
• In python, the moment you assign value to
variable, that’s when data type will be set for
variable.
Numbers in python
There are 3 number types available in python
1. Int :-- it's a whole number(positive or negative) without decimals, unlimited length
2. Float :-- it's a decimal number (positive or negative) containing one or more decimals.
3. complex :-- complex numbers are written with "j" as the imaginary part
Strings
• Strings in python are surrounded by single quote or double quote.
• To get multiple string, we should use three quotes
• String in python are like array of characters. Hence, we can access character in strings using
indexes. Note, doesn’t has character data type
• Since strings are like array of characters, we can loop through them
• In keyword can be used to check certain phrase or character in string
• To concatenate use + symbol.
Slicing strings
• We can return range of characters by using slice syntax.
a = "hello"
Print (a[2:4])
• Use negative index to start slice from end of string
a = "hello"
Print(a[-5:-2)
Format strings in python :
• We cannot combine strings and numbers directly using + symbol
• But we can combine strings and numbers using format() method
• Format() method takes the passed arguments, formats them and
places them in string where place holders {} are there.
X = "am aliya"
Age = 22
Print("am aliya {}" .format(age)
PYTHON Page 2
Escape character
• We will use escape character to insert
character that are in valid in string
• Escape character is a backslash \
followed by character we want to
insert.
Print('hello\nworld')
PYTHON Page 3