Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
suyashi29
GitHub Repository: suyashi29/python-su
Path: blob/master/Python Essentials/Python_basics-1.ipynb
3603 views
Kernel: Python 3 (ipykernel)

Python Basics - Part 1

Welcome to Python Basics. In this notebook, we will learn some fundamental concepts of Python programming. Introduction Python is a popular, high-level programming language known for its simplicity and readability. It is widely used in web development, data analysis, artificial intelligence, scientific computing, and more.

Variables and Print Statement

Variables are used to store information. In Python, you don't need to declare the type explicitly. The print() function is used to display output.

  • type() : use to check class of a declared variable

#Numbers: integer, float, Complex x=2 y=17.2 type(x) type(y) type(3+9j)
complex
a_1="Ashi" _d=24 a="asbhi"
1_a="ashi" ## please note: A variable cannot start with a number or symbol
Cell In[7], line 1 1_a="ashi" ^ SyntaxError: invalid decimal literal
# Example: Variables and print statement name = "Macie" age = 25 height="14.5" hobbies = ["reading", "cycling", "painting"] # List grades = {"Math": 90, "Science": 85} print("Hello, my name is", name) print("I am", age, "years old.")
Hello, my name is Macie I am 25 years old.

Placeholder Formatting in Print Statements

You can format strings using f-strings or the format() method.

# Using f-strings print(f"My name is {name} and I am {age} years old.") print(f"I am {height} feet tall and my favorite hobby is {hobbies[0]}.")
My name is Macie and I am 25 years old. I am 14.5 feet tall and my favorite hobby is reading.
#Using format() method print("My name is {} and I am {} years old.".format(name, age)) print("I scored {math} in Math and {science} in Science.".format(math=grades["Math"], science=grades["Science"]))
My name is Macie and I am 25 years old. I scored 90 in Math and 85 in Science.

Numbers and Operations

Python supports different types of numbers: integers, floats, and complex numbers.

You can perform operations such as addition, subtraction, multiplication, and division.

# Example: Number operations a = 10 b = 3 print("Addition:", a + b) print("Subtraction:", a - b) print("Multiplication:", a * b) print("Division:", a / b) print("Floor Division:", a // b) print("Modulus:", a % b) print("Exponentiation:", a ** b)
Addition: 13 Subtraction: 7 Multiplication: 30 Division: 3.3333333333333335 Floor Division: 3 Modulus: 1 Exponentiation: 1000

Binary Operations

Binary operations are performed on numbers at the bit level.

  • AND (&)

  • OR (|)

  • XOR (^)

  • NOT (~)

  • Shift Left (<<)

  • Shift Right (>>)

000-0 001-1 010-2 011-3 100-4 101-5 101 011 001- AND 111- OR 110- 101 101 0 - 10

# Example: Binary operations x = 5 # 0101 in binary y = 3 # 0011 in binary print("AND:", x & y) print("OR:", x | y) print("XOR:", x ^ y) print("NOT x:", ~x) print("Shift left x by 1:", x << 1) print("Shift right x by 1:", x >> 1)
AND: 1 OR: 7 XOR: 6 NOT x: -6 Shift left x by 1: 10 Shift right x by 1: 2

Input and Output

The input() function allows you to take user input. By default, input is taken as a string.

# Create an interface where user will add their following details" print("Welcome to this site. please enter your details as below") name=input("Name:") age=int(input("Age:")) height=float(input("Height:")) print("***thankyou***")
Welcome to this site. please enter your details as below Name:Asha Age:32 Height:5 ***thankyou***

Hands-on Quick Practice

Try solving the following problems:

  1. Create two variables and print their sum.

  2. Take two numbers from the user and print their product.

  3. Perform binary AND, OR operations on two integers given by the user.

  4. Write a program that asks for your age and prints how old you will be in 5 years.

  5. Create a variable age and assign your age to it.

  6. Print a greeting message using your name and age.

  7. Perform and print the result of 25 % 4.

  8. Take two numbers as input and print their sum.

  9. Use binary operations to find the result of 12 & 7.

# Write your practice code here