0% found this document useful (0 votes)
33 views

P YTHON

The document discusses variables in Python including defining, storing, and printing variables of different data types like integers, floats, strings, and booleans. It also covers getting user input, converting variable types, basic string operations like upper/lower case, find, replace and arithmetic operators.

Uploaded by

parshvasanghvii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

P YTHON

The document discusses variables in Python including defining, storing, and printing variables of different data types like integers, floats, strings, and booleans. It also covers getting user input, converting variable types, basic string operations like upper/lower case, find, replace and arithmetic operators.

Uploaded by

parshvasanghvii
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Write Hello World

• print(“Hello World”)

• Surround the data with quotes ,single or double


• Print helps you give the output.
Variables

• We use variables to temporarily store data in computer’s memory.


price = 10
rating = 4.9
course_name = ‘Python for Beginners’
is_published = True

• In the above example,


• price is an integer (a whole number without a decimal point)
• rating is a float (a number with a decimal point)
• course_name is a string (a sequence of characters)
• is_published is a boolean. Boolean values can be True or False.
Variables
Age = 20
Print (Age)

Try!!
Age=20
Age=30
Print (Age)

Print(“age”)is different.
• Price= 19.5
• First_name = “your name”
• Print (first_name)

• Boolean(Yes or No)
• Is_online = False
• Use capital F.
Activity 1
• We check in a patient name George smith.
• He is 20 years old.
• He Is a new patient.

• Use variables to store these values.


Solution
• P_Name =“George Smith"
• P_age =20
• P_admit="New“
• print (P_Name ,P_age,p_admit)
Receiving Input
• Input function (pre defined)
• Try :

• Input(“What is your name? ”)


• Create a variable and put the input in the variable.

• name = input (“What is your name? ”)


• Print (“Hello ” + name)
• #it combines two strings.
Extend the activity
• Ask persons name
• And their favourite color
• And then print
solution
• name = input (“What is your name? ”)
• Favourite_colour = input (“What is your favourite colour? ”)
Print(names + ‘likes’ + favourite colour)
Converting the type of variable
• Int
• Str
• Bool
• Float
Try: find the error on the second line
• Birth_year = input(“Enter your birth year: “)
• Age = 2024 – (birth_year)
• Print(age)

• It wont work because whenever you use a input function you


always get a string,so if you are expecting a numerical value you
should always convert that string into an integer or float.
Correct ans
• Birth_year = input(“Enter your birth year: “)
• Age = 2024 – int(birth_year)
• Print(age)
Calculator
• First : 10.1
• Second : 20
• Sum : 30.1
• First_num=input("Enter Your First Num. ")
• Sec_num=input("Enter Your Sec Num" ")
• Sum =float(first_num+sec_num)
• Print ("sum is =" + sum)

• First_num=float input("Enter Your First Num. ")


• Sec_num=float input("Enter Your Sec Num" ")
• Sum =(first_num+sec_num)
• Print (“Sum: ” + str(sum))
• Ask a user their weight and convert it into kgs and print on the
terminal
• Weight_lbs = input(“What is your Weight (lbs): “)
• Weight_kg= int(weight_lbs) * 0.45
• Print (weight_kg)
• course = ‘Python’s course for Beginners’
• Ans: course = “Python’s course for Beginners”
• print (course)

• course = “Pythons for “Beginners”


• Ans: course = ‘Pythons for “Beginners” ’
Multiple lines like an email.
• course = '''

• Hi sam

• this is our first message

• thank you,
• the support team.

• '''
• print(course)
We can get individual characters in a string using
square brackets [].
• course = ‘Pythons for Beginners’

• print(Course[0])
Extract few characters instead of 1
• course = ‘Pythons for Beginners’
• print(Course[0:3])
• print(Course[0:])
• print(Course[:3])
Creates A COPY of the variable
• course = ‘Pythons for Beginners’
• another = course[:]
• Print(another)
Excercise
• name=“joseph”
• print(name [1:-1])
len
• course = ‘Pythons for Beginners’
• print( len(course) )
• print (course.upper() )
• print(course)
Dot function- upper and lower case
• course = ‘Pythons for Beginners’
• print (course.upper() )
• Print(course.lower() )
• print(course)
Find operator
• course = ‘Pythons for Beginners’
• print (course.find(‘y’) )
replace
• course = ‘Pythons for Beginners’
• print (course. replace(‘for’, ‘4’) )
Arithmetic operators
•+
•-
•*
•/
• //
• **
• Augumented assignment operator
Foramtted strings
• Strings We can define strings using single (‘ ‘) or double (“ “)
quotes. To define a multi-line string, we surround our string with
tripe quotes (“””).
• We can get individual characters in a string using square brackets
[].
• course = ‘Python for Beginners’ course
• [0] # returns the first character course
• [1] # returns the second character course
• [-1] # returns the first character from the end course
• [-2] # returns the second character from the end
• We can slice a string using a similar notation: course[1:5]
• The above expression returns all the characters starting from the
index position of 1 to 5 (but excluding 5).
• The result will be ytho
• If we leave out the start index, 0 will be assumed. If we leave out
the end index, the length of the string will be assumed.

You might also like