Basic of Python
Here are some basic of python to learn.
- Basic of Python
- Some basics of python
- Print() Function
- Escape Sequence
- Comments
- Variable
- Rule for assigning variable
- String Concatenation
- User Input
- Int() Function
- Two or more input in one line
- String Formatting
- String Indexing
- String Slicing
- Step Argument
- String Methods
- Solving problem with Space
- Find and Replace Method
- String as Immutable
Basic of Python
The syntax of Python is straightforward and resembles that of English. Python's syntax differs from various other programming languages in that it enables programmers to construct applications with fewer lines of code. Python operates on an interpreter system, allowing for the immediate execution of written code. As a result, prototyping can proceed quickly.
Some basics of python
- Print() Function
- Escape Sequence
- Types of Escape sequence
- Comments
- Variable
- Rule for assigning variable
- String Concatenation
- User Input
- Int() Function
- Two or more input in one line
- String Formatting
- String Indexing
- String Slicing
- Step Argument
- String methods
- len() Function
- lower() Method
- Upper() Method
- Tilt() Method
- Count() Method
- Solving problem with Space
- lstrip Method
- rstrip Method
- strip Method
- Find and Replace Method
- String as Immutable
print("Hello Anisha")
print('Hello Anisha')
print("Hello "Anisha"")
print('Hello 'Anisha'')
print("hello 'Anisha'")
print('Hello "Anisha"')
print("Hello \"Anisha\"")
- Types of Escape sequence
\' Single Quote
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
# Single line comments are done using '#' symbol
# example
# This is single line comment
# Multiple line comments are done using ' """ ' ' """" ' symbol
# example
"""
This is a comment
written in
more than just one line
"""
print("Hello Anisha")
a = 7
A = "Anisha"
print(a)
print(A)
x = 7
y = "Anisha"
print(type(x))
print(type(y))
x = "Anisha"
print(x)
# is the same as
x = 'Anisha'
print(x)
Rule for assigning variable
Some of the rules for assigning variable are as follows:
- A variable name must start with a letter or the underscore character
- A variable name cannot start with a number
- A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
- Variable names are case-sensitive (age, Age and AGE are three different variables)
myvar = "Anisha"
my_var = "Anisha"
_my_var = "Anisha"
myVar = "Anisha"
MYVAR = "Anisha"
myvar2 = "Anisha"
#Illegal variable names:
2myvar = "Anisha"
my-var = "Anisha"
my var = "Anisha"
first=("Anisha")
second=("Dhakal")
third=first + second
print(third)
a = "Anisha"
b = "Dhakal"
c = a + " " + b
print(c)
x = 5
y = "John"
print(x + y)
a= " Anisha"
b= 7
print(a * b)
username = input("Enter username:")
print("Username is: " + username)
first=int(input("enter first number"))
second=int(input("enter second number"))
third= first+ second
name,age="Anisha","20" # the input should be in order of variable name
print("hello" + name+"your age is "+"age")
name,age=input("enter your name and age ").split(",")
# .split(","): it is used to give comma
# .split(" "): it is used to add space
txt1 = "My name is {fname}, I'm {age}".format(fname = "John", age = 36)
#numbered indexes:
txt2 = "My name is {0}, I'm {1}".format("John",36)
#empty placeholders:
txt3 = "My name is {}, I'm {}".format("John",36)
print(txt1)
print(txt2)
print(txt3)
name = "Hello, my name is ANISHA."
x = name.index("l")
print(x)
pencil = "era ser"
print(pencil[2:4])
name="Anisha"
print(name[1:4:2])
- len() function It is used to count the length of string.
name= " Anisha dhakal"
print(len(name))
- lower() Method
It is used to change the given strng in lowercase.
We use . in method
name= " Anisha dhakal"
print(name.lower())
- upper() Method It is used to change the given strng in uppercase.
name= " Anisha dhakal"
print(name.upper())
- title() Method It is used to change first letter of string in capital case.
name= " anisha dhakal"
print(name.title())
- count() Method It is used to count string as well as character repetation time.
name= " anisha dhakal"
print(name.count("a"))
- lstrip method It prints all string towards left side, recuding space in left.
name=(" Anisha ")
print(name.lstrip())
- rstrip method It prints all string towards right side, recuding space in right.
name=(" Anisha ")
print(name.rstrip())
- strip method It prints all string removing space in left and right side.
name=(" Anisha ")
print(name.strip())
txt = "Hello, welcome to my world."
x = txt.find("welcome")
print(x)
txt = "I like mangos"
x = txt.replace("mangos", "apples")
print(x)
string="anisha"
string.replace('a','A')
print(string)
string="anisha"
string.replace('a','A')
print(string.replace('a','A',))