
![]() |
@wtf | |
Build your own bio/profile script |
||
3
Replies
23
Views
1 Bookmarks
|
![]() |
@wtf | 29 days |
Today, let's start with the first topic of Python Coding Challenge: *Variables & Data Types* *What is a variable?* Think of a variable as a label on a container that holds some data. You create a variable by giving it a name and putting a value inside. In Python, it looks like this: name = Alex age = 25 height = 5.9 is_student = True - name holds text (called a string) — notice the quotes! - age holds a whole number (integer) - height holds a decimal number (float) - is_student holds a True/False value (boolean) You can check a variable’s type using type(): print(type(name)) class 'str' print(type(age)) class 'int' *Why use variables?* They let you store information so your program can remember it and use it later — like a player's score or your name. *Valid Variable Names* Python has some rules for naming variables: ✅ Must start with a letter or underscore ✅ Can contain letters, numbers, and underscores ❌ Cannot start with a number ❌ Cannot use Python’s built-in keywords like if, while, class, etc. *Examples:* user_name = Amit valid _user2 = Riya valid 2nd_user = Error! ❌ Invalid: starts with number *Let’s build a small project!* We’ll write a simple program that asks for your name, age, and favorite hobby, then prints a fun personalized message. *Here’s the full code:* Ask the user for their name and save it in a variable name = input(What's your name? ) Ask for their age and save it age = input(How old are you? ) Ask for their favorite hobby and save it hobby = input(What's your favorite hobby? ) Print a personalized message using the info collected print(fHey name! So you’re age years old and love hobby. That’s awesome!) *How this works:* input() waits for you to type something and press Enter — whatever you type gets saved in the variable. print() shows a message on the screen. The f means you can include variables inside the message for personalization. |
||
![]() |
@wtf | 28 days |
Now, let’s move on to next topic in the Python coding challenge. *Operators in Python* Operators are special symbols or keywords that perform operations on variables and values. *Types of Operators in Python:* *1. Arithmetic Operators* Used for basic math: + (add), - (subtract), * (multiply), / (divide), // (floor divide), % (modulus), ** (power) a = 10 b = 3 print(a + b) 13 print(a ** b) 1000 *2. Comparison Operators* Used to compare two values: ==, !=, , , =, = x = 5 print(x == 5) True print(x != 3) True *3. Logical Operators* Used to combine conditional statements: and, or, not age = 20 print(age 18 and age 25) True *4. Assignment Operators* Used to assign values to variables: =, +=, -=, *=, /=, etc. score = 10 score += 5 score is now 15 *Mini Project: Build a Simple Calculator* Let’s apply what we’ve learned! *Task: Build a calculator that asks the user to enter two numbers and an operator, then prints the result.* *Approach* 1. Take two numbers from the user. 2. Ask for an operator (+, -, *, /). 3. Perform the operation based on what the user entered. 4. Print the result, or shows Invalid operator! if the input is wrong. *Python Code*: num1 = float(input(Enter first number: )) op = input(Enter operator (+, -, *, /): ) num2 = float(input(Enter second number: )) if op == +: print(num1 + num2) elif op == -: print(num1 - num2) elif op == *: print(num1 * num2) elif op == /: print(num1 / num2) else: print(Invalid operator!) |
||
![]() |
@wtf | 26 days |
*Variables & Data Types* *What is a variable?* Think of a variable as a label on a container that holds some data. You create a variable by giving it a name and putting a value inside. In Python, it looks like this: name = Alex age = 25 height = 5.9 is_student = True - name holds text (called a string) notice the quotes! - age holds a whole number (integer) - height holds a decimal number (float) - is_student holds a True/False value (boolean) You can check a variables type using type(): print(type(name)) class 'str' print(type(age)) class 'int' *Why use variables?* They let you store information so your program can remember it and use it later like a player's score or your name. *Valid Variable Names* Python has some rules for naming variables: &9989; Must start with a letter or underscore &9989; Can contain letters, numbers, and underscores &10060; Cannot start with a number &10060; Cannot use Pythons built-in keywords like if, while, class, etc. *Examples:* user_name = Amit valid _user2 = Riya valid 2nd_user = Error! &10060; Invalid: starts with number *Lets build a small project!* Well write a simple program that asks for your name, age, and favorite hobby, then prints a fun personalized message. *Heres the full code:* Ask the user for their name and save it in a variable name = input(What's your name? ) Ask for their age and save it age = input(How old are you? ) Ask for their favorite hobby and save it hobby = input(What's your favorite hobby? ) Print a personalized message using the info collected print(fHey name! So youre age years old and love hobby. Thats awesome!) *How this works:* input() waits for you to type something and press Enter whatever you type gets saved in the variable. print() shows a message on the screen. The f means you can include variables inside the message for personalization. |
||


