Python Print Function And Variables, Study notes of Computer Programming

Introduction to Python language of programming.

Typology: Study notes

2020/2021

Available from 03/16/2024

THIGHER
THIGHER 🇵🇭

8 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Python print function and variables
Print (“Hello word”)
- Print function print whatever is in between the parentheses.
- Hello world, in this case, is a string.
String
- a sequence of characters either one or many and is always wrapped inside quotation marks
- use single or double quote for string but not single and double quote at the same time
Numeric data:
- Integers – whole numbers
- Float – decimal numbers
- Print function can be used to print integers and float without quotation mark
To store data – variable
Example:
- city = “Kathmandu”
- print (city)
- city variable stores the string Kathmandu
- there should be no quotation on the variable itself
- city = “New York”
- print (city)
- possible to assign a value of one variable to another.
Example:
Destination_city = “New York”
city = destination_city
print (city)
pf3
pf4
pf5

Partial preview of the text

Download Python Print Function And Variables and more Study notes Computer Programming in PDF only on Docsity!

Python print function and variables Print (“Hello word”)

  • Print function print whatever is in between the parentheses.
  • Hello world, in this case, is a string. String
  • a sequence of characters either one or many and is always wrapped inside quotation marks
  • use single or double quote for string but not single and double quote at the same time Numeric data:
  • Integers – whole numbers
  • Float – decimal numbers
  • Print function can be used to print integers and float without quotation mark To store data – variable Example:
  • city = “Kathmandu”
  • print (city)
  • city variable stores the string Kathmandu
  • there should be no quotation on the variable itself
  • city = “New York”
  • print (city)
  • possible to assign a value of one variable to another. Example: Destination_city = “New York” city = destination_city print (city)

My_favorite_number = 5 Print (My_favorite_number) Pi = 3. Print (pi) My_favorite_number = pi Print (My_favorite_number) To print more than one variable and data in a single print statement, we can separate them by commas Example: Print (“City:”, “Kathmandu”) We can print variables and string in a single statement Example: city = “Kathmandu” Print (“City:”, city) city = “Kathmandu” kfc_locations = 3 print (“City:”, city, “KFC Locations:”, kfc_locations)

Number = input (“Enter number: “) Print (type(Number)) Number1 = 5 Print (type(Number1)) Number2 = 5. Print (type(Number2)) Number = input (“Enter a number: “) Print (Number) **It is not possible to take integer and float input using the input function however, we can convert the string to a number after we take the input Number = input (“Enter a number: “) Number = int(Number) Print (Number) Number = input (“Enter a number: “) Number = int(Number) Print (type(Number)) Number = int(input (“Enter a number: “)) Print (type(Number)) Number = float(input (“Enter a number: “)) Print (type(Number))

However, python cannot convert every string to numbers (non-numeric) Number = float(input(“Enter a number: “)) Print (type(Number)) What we have learned?

  • We use input() function to get user input.
  • Input() always takes input in the string format
  • To convert string to an integer, we use int().
  • To convert string to a float, we use float().
  • We can only convert numeric strings to numbers.