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

? Python Programming Basics

This document provides an overview of Python programming basics, covering topics such as printing output, arithmetic operations, variables, data types, type conversion, and user input. It explains the syntax for various operations and functions, including the use of escape characters, formatted strings, and multiple assignments. Additionally, it highlights the importance of input handling and provides examples for both string and numeric inputs.

Uploaded by

bubble차
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

? Python Programming Basics

This document provides an overview of Python programming basics, covering topics such as printing output, arithmetic operations, variables, data types, type conversion, and user input. It explains the syntax for various operations and functions, including the use of escape characters, formatted strings, and multiple assignments. Additionally, it highlights the importance of input handling and provides examples for both string and numeric inputs.

Uploaded by

bubble차
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

📌 Python Programming Basics

Python is a high-level, interpreted programming language that is beginner-friendly due to


its simple syntax and readability.

🖥️ 1. Printing Output in Python


The print() function is used to display output on the screen.

Basic Syntax
python
CopyEdit
print("Hello, World!") # Outputs: Hello, World!

Different Ways to Print Strings

Python allows single ('), double (") and triple (''' """) quotes:

python
CopyEdit
print('Hello')
print("Hello")
print('''Hello''')
print("""Hello""")

All of these will output Hello.

Escape Characters

Used to format strings properly:

Escape Description Example Output


Character

\n New line print("Hello\nWorld") → Hello (new line) World

\t Tab space print("Hello\tWorld") → Hello World

\\ Backslash print("This is a backslash: \\") → This is a


backslash: \
\' Single quote print('It\'s a good day') → It's a good day

\" Double print("He said \"Hello\"") → He said "Hello"


quote

Multi-Line Strings
python
CopyEdit
print("""This is
a multi-line
string.""")

This prints exactly as written, across multiple lines.

🔢 2. Working with Numbers (Arithmetic Operations)


Python supports mathematical operations like addition, subtraction, multiplication, and more.

Basic Arithmetic
Operato Description Example Output
r

+ Addition print(5 + 8
3)

- Subtraction print(10 - 6
4)

* Multiplication print(2 * 6
3)

/ Division (float) print(10 / 3.3333...


3)

// Floor Division print(10 // 3


3)

% Modulus print(10 % 1
(Remainder) 3)

** Exponentiation print(2 ** 8
3)
🔹 Floor Division (//): Removes the decimal part, keeping only the integer.​
🔹 Modulus (%): Useful for checking even/odd numbers.
python
CopyEdit
print(10 % 2) # 0 (even)
print(15 % 2) # 1 (odd)

📝 3. Variables and Data Types


A variable is a container that stores values.

Declaring Variables
python
CopyEdit
x = 5
y = "Hello"
z = 3.14
print(x, y, z)

●​ x is an integer
●​ y is a string
●​ z is a float

Variable Naming Rules

✅ Allowed:​
✔️ my_var = 10​
✔️ age = 25​
✔️ name_of_person = "Alice"
❌ Not Allowed:​
🚫 2name = "John" (Cannot start with a number)​
🚫 my-var = 10 (Cannot use -, use _ instead)​
🚫 class = "CS101" (Avoid Python keywords like class, if, def)

🔤 4. Data Types
Python has four primary data types:
Type Example Description

int 10, -5, 0 Whole numbers

floa 3.14, -2.5 Decimal


t numbers

str "Hello", Text data


'Python'

bool True, False Boolean values

Checking Data Type

Use type() function:

python
CopyEdit
a = 10
b = 3.5
c = "Python"
print(type(a)) # Output: <class 'int'>
print(type(b)) # Output: <class 'float'>
print(type(c)) # Output: <class 'str'>

🔄 5. Type Conversion
Python allows changing data types using:

●​ int(), float(), str(), bool()

Examples
python
CopyEdit
num = "100"
print(int(num) + 50) # Converts "100" (string) to 100 (integer),
output: 150

num = 10.5
print(int(num)) # Converts 10.5 (float) to 10 (integer)
🖨️ 6. Printing with Formatting
Using sep and end

●​ sep controls the separator between words.


●​ end controls what happens at the end of a line.

python
CopyEdit
print("Hello", "World", sep="-") # Output: Hello-World
print("Python", end=" ")
print("Programming") # Output: Python Programming

Formatted Strings (f-strings)

Allows inserting values directly into strings.

python
CopyEdit
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")

✍️ 7. Variables Assignment Tricks


Multiple Assignments
python
CopyEdit
x, y, z = 1, 2, 3
print(x, y, z) # Output: 1 2 3

Same Value Assignment


python
CopyEdit
a = b = c = 10
print(a, b, c) # Output: 10 10 10
🎯 8. More on Strings
Strings can be concatenated (joined) using + and repeated using *.

python
CopyEdit
greeting = "Hello"
name = "Alice"
print(greeting + " " + name) # Output: Hello Alice
print(name * 3) # Output: AliceAliceAlice

🔹 String Indexing
python
CopyEdit
word = "Python"
print(word[0]) # P
print(word[-1]) # n

🔹 String Slicing
python
CopyEdit
print(word[0:3]) # Pyt (from index 0 to 2)
print(word[:4]) # Pyth (from start to index 3)
print(word[2:]) # thon (from index 2 to end)

🚀 Quick Recap
✅ print() function outputs text​
✅ Arithmetic operations include + - * / // % **​
✅ Variables store values (int, float, str, bool)​
✅ f-strings make printing dynamic​
✅ Strings can be concatenated, indexed, and sliced​
✅ Type conversion: int(), float(), str()​
✅ sep and end customize print output
Would you like practice problems to test your understanding? 😊
You said:
so we also learned input codes, how do that work?

ChatGPT said:

📌 User Input in Python (input())


The input() function allows a program to take input from the user.

🖥️ Basic Usage
python
CopyEdit
name = input("Enter your name: ")
print("Hello, " + name + "!")

🔹 How it works?
●​ The input() function displays a prompt ("Enter your name: ") and waits for
the user to type something.
●​ Whatever the user types is stored in the name variable.
●​ The program then prints "Hello, [name]!" using string concatenation.

🔢 Taking Numeric Input


By default, input() takes everything as a string. If you need numbers, you must convert
them.

Integer Input (int())


python
CopyEdit
age = int(input("Enter your age: "))
print("Next year, you will be", age + 1)

✅ If the user types 21, age becomes 21 (integer).​


🚨 If the user types "twenty", an error occurs because "twenty" cannot be converted
to int.

Float Input (float())


python
CopyEdit
height = float(input("Enter your height in meters: "))
print("Your height is", height, "meters")
✅ If the user types 1.75, height becomes 1.75 (float).

📝 Multiple Inputs in One Line


Use split() to take multiple inputs at once.

python
CopyEdit
x, y = input("Enter two numbers: ").split()
print("First number:", x)
print("Second number:", y)

🔹 How it works?
●​ User enters: 5 10
●​ split() separates the numbers at the space.
●​ x gets "5", y gets "10".

🚨 Convert to integers if needed:


python
CopyEdit
x, y = map(int, input("Enter two numbers: ").split())
print("Sum:", x + y)

📌 Formatted Input
You can use formatted strings (f-strings) with input():

python
CopyEdit
name = input("What is your name? ")
print(f"Hello, {name}! Nice to meet you.")

🔥 Quick Recap
Input Type Code Example Notes
String name = input("Enter name: ") Default type is string

Integer num = int(input("Enter a Convert string to integer


number: "))

Float num = float(input("Enter a Convert string to float


decimal: "))

Multiple x, y = input().split() Splits input into multiple


Inputs variables

Would you like practice exercises on input? 😊


You said:
Set A - Spring_2024_CSC100_Mid Term Question.docx.pdf
PDF
Set B - Spring_2024_CSC100_Mid Term Question.docx.pdf
PDF
SampleQ - Spring_2024_CSC100_Mid Term Question.pdf
PDF
solve

ChatGPT said:

I will now begin solving the Set A and Set B midterm questions step by step. I will provide
detailed explanations for each solution, including calculations, coding solutions, and
conceptual answers.

I will start by solving the MCQs, Short Questions, and Coding Exercises. Give me a
moment to prepare the answers.

I'll first create a structured document with all the solutions for both Set A and Set B. This


will include:​


MCQ Answers with Explanations​
Short Questions (Binary conversion, security, ML, networking, storage


calculations, etc.)​
Coding Exercises (Python solutions with comments)

Give me a moment to compile everything.

Cse100 Midterm Solutions

I've compiled all the Set A & Set B solutions with detailed explanations, binary

🚀
conversions, security analysis, machine learning applications, and Python coding
fixes. Let me know if you need any clarifications or extra practice!

You said:

You might also like