Python Tutorial for Beginners Part 2
Strings:-
Strings include letters, special characters, spaces, digits, etc.
It is enclosed in single quotes ( ‘ ‘ ) or double quotes ( ” ” ).str = "Cyber Connaught"
We can concatenate strings
str = "Cyber Connaught"
welcome_str = "Welcome to our website"
concate_str = welcome_str + str
print(concate_str)
> Welcome to our websiteCyber Connaught
Covered Topics
- Strings
- Indexing
- Slicing
- Input
- Comparison Operators
- Logical Operators
To get it in a proper way
Concate_str = welcome_str + " " + str
- Just give some space in
welcome_str
at end
welcome_str = "Welcome to our website"
print(concate_str = welcome_str + " " + str)
> Welcome to our website Cyber Connaught
welcome_str = "Welcome to our website "
concate_str = welcome_str + str
print(concate_str)
> Welcome to our website Cyber Connaught
One of the operations on a string is defined in python docs.
str = "Conrado"
str_3 = "My name is " + str * 3 + "!"
> My name is ConradoConradoConrado!
We can calculate the length of the string by len().
name = "Conrado"
len(str)
> 7
sur_name = "Torres"
full_name = name + " " + sur_name
len(full_name)
> 14
Indexing:-
We can get a specific character as a variable or output in a string by indexing the string. An index is a position in a string. In Python indexing start from 0.
The first character is at an index of 0
The second character is at index 1
and goes on…..
name = "Conrado Torres"
C | o | n | r | a | d | o | T | o | r | r | e | s | |||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | |
-15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
print(name[2])
>n
print(name[6])
>o
print(name[12])
>e
How do you print the last character of a string?str = "pneumonoultramicroscopicsilicovolcanokoniosis"
We can use two simple methods to get the last character.
char = str[len(str) - 1]
print(char)
> s
print(str[-1])
> s
python uses negative indexing to count from the end.
Slicing:-
We can get a subset of characters by slicing.
steps:-
- get the starting index of the slice
- separate with a column
- get the ending index of the slice
str [ i : j ] gives you characters at index
i , i + 1 , i + 2 , … , j – 1
Once go through the following tables and the following problems.
name = "Cyber Connaught"
C | y | b | e | r | C | o | n | n | a | u | g | h | t | |||
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | |
-16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
Examples –
name[6:13] > ‘Connaug’ | name[0:5] > ‘Cyber’ | name[6:15] > ‘Connaught’ |
name[6:-2] > ‘Connaug’ | name[:5] > ‘Cyber’ | name[6:] > ‘Connaught’ |
1) | name [4:7] ‘r C’ | name[-11:-8] ‘r C’ | name[4:-8] ‘r C’ | name[-11:7] ‘r C’ |
2) | name[:] ‘Cyber Connaught’ | name[::] ‘Cyber Connaught’ | name[0:] ‘Cyber Connaught’ | name[:15] ‘Cyber Connaught’ |
Input:-
variable = input("Write some thing you want from user")
Example –
name = input("Enter your name..")
#input statement
> Enter your name.. Cyber Connaught #you have to enter name
# Now, name = "Cyber Connaught"
print("Your name is " + name)
> Your name is Cyber Connaught
Input gives you a string so you must cast if working with numbers.
Examples –
num = input("Ënter the number ")
> Enter the number 3
print(3*num)
> 333
num = int(input("Ënter the number "))
> Enter the number 3
print(3*num)
> 9
Write a program that will ask your name and your age and then print it out like
“My name is ‘name’ and I am ‘age’ years old”
name = input("Enter your name ")
age = input("Enter your age ")
print("My name is " + name + " and I am " + age + " years old.")
Comparison Operators:-
Let’s take i and j variable names. The output of the comparison evaluates to a Boolean value.
1. | i > j | i greater than j | 5 > 3 | True | |
2. | i >= j | i greater than or equal to j | 5 >= 3 | 5 >= 5 | True |
3. | i < j | i less than j | 3 < 5 | True | |
4. | i <= j | i less than or equal to j | 3 <= 5 | 5 <= 5 | True |
5. | i == j | i equal to j | 3 == 3 | 3 == 3.0 | True |
6. | i != j | i not equal to j | 3 != 5 | True |
Logical Operators:-
There are three operators not, and, or
- not negates the Boolean value
- and returns
True
only if both statements are True. - or returns
False
only if both statements are False.
Just here for better understanding take :- ( True = 1 ) , ( False = 0 )
Sr.no | a | b | not a | not b | a and b | a or b |
---|---|---|---|---|---|---|
1 | 0 | 0 | 1 | 1 | 0 | 0 |
2 | 0 | 1 | 1 | 0 | 0 | 1 |
3 | 1 | 0 | 0 | 1 | 0 | 1 |
4 | 1 | 1 | 0 | 0 | 1 | 1 |
Examples –
a , b ,c = 6 , 8 , 4
a > b
## False
not (a > b)
## True
(a > b) and (b > a)
## False
(a > b) or (b > a)
## True
(a < c) or ((b > c) and not (a < c))
## True
work_time = 15
sleep_time = 8
print(sleep_time < work_time)
## True
work_time = 15
sleep_time = 8
print(sleep_time > work_time)
## False
sports = True
games = False
both = sports and games
print(both)
## False
Here sports can get its value by another function or equation by a sequence of logical steps.
sports = 3>2 ( Equation )
games = False
both = sports and games
print(both)
## False